1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2007-2013 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 "ownereditdlg.h"
21 
22 #include "config.h"
23 
24 #include <QCheckBox>
25 #include <QDialogButtonBox>
26 #include <QGridLayout>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QPushButton>
30 
31 #include <licq/contactlist/owner.h>
32 #include <licq/contactlist/usermanager.h>
33 #include <licq/plugin/pluginmanager.h>
34 #include <licq/protocolmanager.h>
35 
36 #include "config/iconmanager.h"
37 #include "core/messagebox.h"
38 #include "helpers/support.h"
39 #include "widgets/skinnablelabel.h"
40 #include "widgets/specialspinbox.h"
41 
42 using namespace LicqQtGui;
43 /* TRANSLATOR LicqQtGui::OwnerEditDlg */
44 
OwnerEditDlg(unsigned long ppid,QWidget * parent)45 OwnerEditDlg::OwnerEditDlg(unsigned long ppid, QWidget* parent)
46   : QDialog(parent),
47     myPpid(ppid),
48     myNewOwner(true),
49     mySetStatus(Licq::User::OfflineStatus)
50 {
51   init();
52   show();
53 }
54 
OwnerEditDlg(const Licq::UserId & ownerId,unsigned setStatus,const QString & autoMessage,QWidget * parent)55 OwnerEditDlg::OwnerEditDlg(const Licq::UserId& ownerId, unsigned setStatus, const QString& autoMessage, QWidget* parent)
56   : QDialog(parent),
57     myOwnerId(ownerId),
58     myPpid(ownerId.protocolId()),
59     myNewOwner(false),
60     mySetStatus(setStatus),
61     myAutoMessage(autoMessage)
62 {
63   init();
64 
65   {
66     Licq::OwnerReadGuard o(myOwnerId);
67     if (!o.isLocked())
68     {
69       close();
70       return;
71     }
72 
73     edtId->setText(o->accountId().c_str());
74     edtId->setEnabled(false);
75     edtPassword->setText(QString::fromLocal8Bit(o->password().c_str()));
76     chkSave->setChecked(o->SavePassword());
77     myHostEdit->setText(QString::fromLocal8Bit(o->serverHost().c_str()));
78     myPortSpin->setValue(o->serverPort());
79   }
80 
81   show();
82 }
83 
init()84 void OwnerEditDlg::init()
85 {
86   Support::setWidgetProps(this, "OwnerEdit");
87   setAttribute(Qt::WA_DeleteOnClose, true);
88   setWindowTitle(tr("Edit Account"));
89 
90   QGridLayout* lay = new QGridLayout(this);
91   lay->setColumnStretch(2, 2);
92   lay->setColumnMinimumWidth(1, 8);
93 
94   SkinnableLabel* protocolName = new SkinnableLabel();
95 
96   edtId = new QLineEdit();
97   connect(edtId, SIGNAL(returnPressed()), SLOT(slot_ok()));
98 
99   edtPassword = new QLineEdit();
100   edtPassword->setEchoMode(QLineEdit::Password);
101   connect(edtPassword, SIGNAL(returnPressed()), SLOT(slot_ok()));
102 
103   myHostEdit = new QLineEdit();
104 #if (QT_VERSION >= QT_VERSION_CHECK(4, 7, 0))
105   myHostEdit->setPlaceholderText(tr("Protocol default"));
106 #endif
107   myPortSpin = new SpecialSpinBox(0, 0xffff, tr("Auto"));
108   myPortSpin->setValue(0);
109 
110   int i = 0;
111   QLabel* lbl;
112 
113 #define ADDWIDGET(name, widget) \
114   lbl = new QLabel(name); \
115   lbl->setBuddy(widget); \
116   lay->addWidget(lbl, i, 0); \
117   lay->addWidget(widget, i++, 2)
118 
119   ADDWIDGET(tr("Protocol:"), protocolName);
120   ADDWIDGET(tr("&User ID:"), edtId);
121   ADDWIDGET(tr("&Password:"), edtPassword);
122 
123   chkSave = new QCheckBox(tr("&Save Password"));
124   lay->addWidget(chkSave, i++, 0, 1, 3);
125 
126   ADDWIDGET(tr("S&erver:"), myHostEdit);
127   ADDWIDGET(tr("P&ort:"), myPortSpin);
128 
129 #undef ADDWIDGET
130 
131   lay->setRowStretch(i++, 2);
132 
133   QDialogButtonBox* buttons = new QDialogButtonBox();
134   buttons->addButton(QDialogButtonBox::Ok);
135   buttons->addButton(QDialogButtonBox::Cancel);
136   connect(buttons, SIGNAL(accepted()), SLOT(slot_ok()));
137   connect(buttons, SIGNAL(rejected()), SLOT(close()));
138   lay->addWidget(buttons, i++, 0, 1, 3);
139 
140 
141   Licq::ProtocolPlugin::Ptr protocol = Licq::gPluginManager.getProtocolPlugin(myPpid);
142   if (protocol.get() != NULL)
143     protocolName->setText(protocol->name().c_str());
144   protocolName->setPrependPixmap(IconManager::instance()->iconForProtocol(myPpid));
145 }
146 
slot_ok()147 void OwnerEditDlg::slot_ok()
148 {
149   if (myNewOwner)
150   {
151     QString id = edtId->text();
152     if (id.isEmpty())
153     {
154       InformUser(this, tr("User ID field cannot be empty."));
155       return;
156     }
157 
158     myOwnerId = Licq::UserId(myPpid, id.toLocal8Bit().constData());
159     Licq::gUserManager.addOwner(myOwnerId);
160   }
161 
162   QString pwd = edtPassword->text();
163 
164   {
165     Licq::OwnerWriteGuard o(myOwnerId);
166     if (!o.isLocked())
167       return;
168 
169     o->setPassword(pwd.toLocal8Bit().constData());
170     o->SetSavePassword(chkSave->isChecked());
171     o->setServer(myHostEdit->text().toLocal8Bit().constData(), myPortSpin->value());
172     o->save(Licq::Owner::SaveOwnerInfo);
173   }
174 
175   if (mySetStatus != Licq::User::OfflineStatus)
176     Licq::gProtocolManager.setStatus(myOwnerId, mySetStatus,
177         (myAutoMessage.isNull() ? Licq::gProtocolManager.KeepAutoResponse : myAutoMessage.toUtf8().constData()));
178 
179   close();
180 }
181