comparison settings.cpp @ 2:fee291c8d42a

settings: profiles management first draft
author Goffi <goffi@goffi.org>
date Mon, 08 Aug 2011 10:30:23 +0200
parents parameters.cpp@22b44846b04b
children 220e5619bf87
comparison
equal deleted inserted replaced
1:2e015f69f756 2:fee291c8d42a
1 /*
2 Bellaciao: a Salut à Toi frontend
3 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "settings.h"
20
21 using namespace Qt;
22
23 Settings* Settings::_instance = 0;
24
25 ProfileListWidgetItem::ProfileListWidgetItem(const QString & profile, QListWidget * parent, bool isDefault)
26 :QListWidgetItem(profile, parent)
27 {
28 m_profile = profile;
29 if (isDefault) {
30 setText(text() + QObject::tr(" (default)"));
31 QFont _font = font();
32 _font.setWeight(QFont::Bold);
33 setFont(_font);
34 }
35
36 }
37
38 const QString& ProfileListWidgetItem::getProfile()
39 {
40 return m_profile;
41 }
42
43 Settings::Settings(QWidget *parent)
44 : QWidget(parent)
45 {
46 setupUi(this);
47
48 this->setAttribute(WA_DeleteOnClose);
49
50 //We fill the profiles list
51 m_bridge = Bridge::getBridge();
52 const QList<QString>& profiles = m_bridge->getProfilesList();
53 const QString& default_profile = m_bridge->getProfileName("@DEFAULT@");
54 foreach(const QString& profile, profiles) {
55 ProfileListWidgetItem *item = new ProfileListWidgetItem(profile, profileList, profile==default_profile);
56 item->setFlags(item->flags() | ItemIsUserCheckable);
57 item->setCheckState(Checked);
58 }
59
60 connect(profileList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(profileChanged(QListWidgetItem*)));
61 connect(profileList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(profileCurrentChanged(QListWidgetItem*, QListWidgetItem*)));
62
63 //Profiles manipulation buttons
64 connect(buttonsGroupConfirm, SIGNAL(accepted()), this, SLOT(saveSettings()));
65
66 //Confirmation buttons
67 connect(buttonsGroupConfirm, SIGNAL(accepted()), this, SLOT(saveSettings()));
68 connect(buttonsGroupConfirm, SIGNAL(rejected()), this, SLOT(cancelSettings()));
69 }
70
71 Settings::~Settings()
72 {
73 _instance = 0;
74 }
75
76 Settings* Settings::getDialog() {
77 if (_instance == 0) {
78 _instance = new Settings;
79 }
80
81 return _instance;
82 }
83
84 //Profile settings slots
85 void Settings::profileChanged(QListWidgetItem* item)
86 {
87 qDebug() << "profileChanged";
88 }
89
90 void Settings::profileCurrentChanged(QListWidgetItem* current, QListWidgetItem* previous)
91 {
92 if (!current) {
93 profileData->setEnabled(false);
94 customServerGroup->setEnabled(false);
95 customServerGroup->setChecked(false);
96 buttonDelete->setEnabled(false);
97 buttonSetDefault->setEnabled(false);
98 jidField->clear();
99 passwordField->clear();
100 serverField->clear();
101 portField->clear();
102 return;
103 }
104
105 ProfileListWidgetItem *item = dynamic_cast<ProfileListWidgetItem*>(current);
106
107 profileData->setEnabled(true);
108 customServerGroup->setEnabled(true);
109 jidField->setEnabled(true);
110 passwordField->setEnabled(true);
111 buttonDelete->setEnabled(true);
112 buttonSetDefault->setEnabled(true);
113 const QString& jid = m_bridge->getParamA("JabberID", "Connection", "value", item->getProfile());
114 const QString& password = m_bridge->getParamA("Password", "Connection", "value", item->getProfile());
115 jidField->setText(jid);
116 passwordField->setText(password);
117 }
118
119
120 void Settings::on_buttonNew_clicked() {
121 bool ok;
122 QString name = QInputDialog::getText(this, tr("New profile"), tr("Please enter profile name: "), QLineEdit::Normal, QString(), &ok);
123 if (ok && !name.isEmpty()) {
124 int success = m_bridge->createProfile(name);
125 ProfileListWidgetItem *item;
126
127 switch (success) {
128 case 0: //success
129 item = new ProfileListWidgetItem(name, profileList);
130 item->setFlags(item->flags() | ItemIsUserCheckable);
131 item->setCheckState(Checked);
132 break;
133 case 1: //profile name conflict
134 QMessageBox::warning(this, tr("Profile name conflict"), tr("A profile with this name already exists. Please choose an other name"));
135 break;
136 default:
137 QMessageBox::warning(this, tr("Profile creation error"), tr("Can't create profile"));
138
139 }
140 }
141 }
142
143
144 void Settings::on_buttonDelete_clicked() {
145 ProfileListWidgetItem *item = dynamic_cast<ProfileListWidgetItem*>(profileList->currentItem());
146
147 if ( QMessageBox::question (
148 this,
149 tr("Delete profile?"),
150 tr("Do you really want to delete profile \"%1\" ?").arg(item->getProfile()),
151 QMessageBox::Yes | QMessageBox::No,
152 QMessageBox::No
153 ) == QMessageBox::Yes
154 )
155 {
156 int success = m_bridge->deleteProfile(item->getProfile());
157 if (success==0)
158 delete(item);
159 else
160 QMessageBox::warning(this, tr("Profile deletion error"), tr("Can't delete profile"));
161 }
162
163
164 }
165
166
167 //Global settings slots
168 void Settings::saveSettings()
169 {
170 }
171
172 void Settings::cancelSettings()
173 {
174 this->close();
175 }