1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2010-2012 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "addgroupdlg.h"
21 
22 #include <QDialogButtonBox>
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QLineEdit>
26 
27 #include <licq/contactlist/group.h>
28 #include <licq/contactlist/usermanager.h>
29 
30 #include "config/contactlist.h"
31 #include "contactlist/contactlist.h"
32 #include "helpers/support.h"
33 #include "widgets/groupcombobox.h"
34 
35 using namespace LicqQtGui;
36 /* TRANSLATOR LicqQtGui::AddGroupDlg */
37 
AddGroupDlg(QWidget * parent)38 AddGroupDlg::AddGroupDlg(QWidget* parent)
39   : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
40 {
41   Support::setWidgetProps(this, "AddGroupDialog");
42   setWindowTitle(tr("Licq - Add Group"));
43   setAttribute(Qt::WA_DeleteOnClose, true);
44 
45   QGridLayout* layDialog = new QGridLayout(this);
46 
47   QLabel* nameLabel = new QLabel(tr("&Group name:"));
48   myNameEdit = new QLineEdit();
49   connect(myNameEdit, SIGNAL(returnPressed()), SLOT(ok()));
50   nameLabel->setBuddy(myNameEdit);
51 
52   layDialog->addWidget(nameLabel, 0, 0);
53   layDialog->addWidget(myNameEdit, 0, 1);
54 
55   QLabel* positionLabel = new QLabel(tr("&Position:"));
56   myPositionCombo = new GroupComboBox(true);
57   myPositionCombo->setCurrentIndex(myPositionCombo->count() - 1);
58   positionLabel->setBuddy(myPositionCombo);
59 
60   // Get current active group and set as default
61   if (Config::ContactList::instance()->groupId() < ContactListModel::SystemGroupOffset)
62     myPositionCombo->setCurrentGroupId(Config::ContactList::instance()->groupId());
63 
64   layDialog->addWidget(positionLabel, 1, 0);
65   layDialog->addWidget(myPositionCombo, 1, 1);
66 
67   QDialogButtonBox* buttons = new QDialogButtonBox(
68       QDialogButtonBox::Ok |
69       QDialogButtonBox::Cancel);
70   connect(buttons, SIGNAL(accepted()), SLOT(ok()));
71   connect(buttons, SIGNAL(rejected()), SLOT(close()));
72 
73   layDialog->addWidget(buttons, 2, 0, 1, 2);
74 
75   myNameEdit->setFocus();
76   show();
77 }
78 
ok()79 void AddGroupDlg::ok()
80 {
81   QString name = myNameEdit->text().trimmed();
82   if (name.isEmpty())
83     return;
84 
85   int groupId = Licq::gUserManager.AddGroup(name.toLatin1().constData());
86 
87   if (groupId != 0)
88   {
89     int afterGroupId = myPositionCombo->currentGroupId();
90     int sortIndex = -1;
91     if (afterGroupId == -1)
92     {
93       sortIndex = 0;
94     }
95     else
96     {
97       Licq::GroupReadGuard g(afterGroupId);
98       if (g.isLocked())
99         sortIndex = g->sortIndex() + 1;
100     }
101     if (sortIndex != -1)
102       Licq::gUserManager.ModifyGroupSorting(groupId, sortIndex);
103   }
104 
105   close();
106 }
107