1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2007-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 "status.h"
21 
22 #include "config.h"
23 
24 #include <boost/foreach.hpp>
25 
26 #include <QComboBox>
27 #include <QGridLayout>
28 #include <QGroupBox>
29 #include <QHBoxLayout>
30 #include <QLabel>
31 #include <QPushButton>
32 #include <QVBoxLayout>
33 
34 #include <licq/contactlist/user.h>
35 #include <licq/sarmanager.h>
36 
37 #include "config/general.h"
38 
39 #include "core/mainwin.h"
40 
41 #include "widgets/mledit.h"
42 #include "widgets/specialspinbox.h"
43 
44 #include "settingsdlg.h"
45 
46 using Licq::SarManager;
47 using Licq::User;
48 using Licq::gSarManager;
49 using namespace LicqQtGui;
50 /* TRANSLATOR LicqQtGui::Settings::Status */
51 
Status(SettingsDlg * parent)52 Settings::Status::Status(SettingsDlg* parent)
53   : QObject(parent)
54 {
55   parent->addPage(SettingsDlg::StatusPage, createPageStatus(parent),
56       tr("Status"));
57 
58   load();
59 }
60 
createPageStatus(QWidget * parent)61 QWidget* Settings::Status::createPageStatus(QWidget* parent)
62 {
63   myAutoAwayBox = new QGroupBox(tr("Auto Change Status"));
64   myAutoAwayLayout = new QGridLayout(myAutoAwayBox);
65   myAutoAwayLayout->setColumnMinimumWidth(2, 20);
66 
67   myAutoAwayLabel = new QLabel(tr("Auto Away:"));
68   myAutoAwayLabel->setToolTip(tr("Number of minutes of inactivity after which to "
69         "automatically be marked \"away\".  Set to \"0\" to disable."));
70   myAutoAwayLayout->addWidget(myAutoAwayLabel, 0, 0);
71 
72   myAutoAwaySpin = new SpecialSpinBox(0, 99, tr("Never"));
73   myAutoAwayLayout->addWidget(myAutoAwaySpin, 0, 1);
74 
75   myAutoAwayMessCombo = new QComboBox();
76   myAutoAwayLayout->addWidget(myAutoAwayMessCombo, 0, 3);
77 
78   myAutoNaLabel = new QLabel(tr("Auto N/A:"));
79   myAutoNaLabel->setToolTip(tr("Number of minutes of inactivity after which to "
80         "automatically be marked \"not available\".  Set to \"0\" to disable."));
81   myAutoAwayLayout->addWidget(myAutoNaLabel, 1, 0);
82 
83   myAutoNaSpin = new SpecialSpinBox(0, 99, tr("Never"));
84   myAutoAwayLayout->addWidget(myAutoNaSpin, 1, 1);
85 
86   myAutoNaMessCombo = new QComboBox();
87   myAutoAwayLayout->addWidget(myAutoNaMessCombo, 1, 3);
88 
89   myAutoOfflineLabel = new QLabel(tr("Auto Offline:"));
90   myAutoOfflineLabel->setToolTip(tr("Number of minutes of inactivity after which to "
91         "automatically go offline.  Set to \"0\" to disable."));
92   myAutoAwayLayout->addWidget(myAutoOfflineLabel, 2, 0);
93 
94   myAutoOfflineSpin = new SpecialSpinBox(0, 99, tr("Never"));
95   myAutoAwayLayout->addWidget(myAutoOfflineSpin, 2, 1);
96 
97   buildAutoStatusCombos(1);
98 
99 
100   myDefRespMsgBox = new QGroupBox(tr("Default Auto Response Messages"));
101   myDefRespMsgLayout = new QGridLayout(myDefRespMsgBox);
102 
103   mySarGroupLabel = new QLabel(tr("Status:"));
104   myDefRespMsgLayout->addWidget(mySarGroupLabel, 0, 0);
105 
106   mySarGroupCombo = new QComboBox();
107   mySarGroupCombo->addItem(User::statusToString(User::AwayStatus).c_str(), SarManager::AwayList);
108   mySarGroupCombo->addItem(User::statusToString(User::NotAvailableStatus).c_str(), SarManager::NotAvailableList);
109   mySarGroupCombo->addItem(User::statusToString(User::OccupiedStatus).c_str(), SarManager::OccupiedList);
110   mySarGroupCombo->addItem(User::statusToString(User::DoNotDisturbStatus).c_str(), SarManager::DoNotDisturbList);
111   mySarGroupCombo->addItem(User::statusToString(User::FreeForChatStatus).c_str(), SarManager::FreeForChatList);
112   connect(mySarGroupCombo, SIGNAL(activated(int)), SLOT(sarGroupChanged(int)));
113   myDefRespMsgLayout->addWidget(mySarGroupCombo, 0, 1);
114 
115   mySarMsgLabel = new QLabel(tr("Preset slot:"));
116   myDefRespMsgLayout->addWidget(mySarMsgLabel, 1, 0);
117 
118   mySarMsgCombo = new QComboBox();
119   mySarMsgCombo->setEditable(true);
120   mySarMsgCombo->setInsertPolicy(QComboBox::InsertAtCurrent);
121   connect(mySarMsgCombo, SIGNAL(activated(int)), SLOT(sarMsgChanged(int)));
122   myDefRespMsgLayout->addWidget(mySarMsgCombo, 1, 1);
123 
124   mySartextEdit = new MLEdit(true);
125   myDefRespMsgLayout->addWidget(mySartextEdit, 2, 0, 1, 3);
126 
127   QHBoxLayout* buttons = new QHBoxLayout();
128 
129   mySarhintsButton = new QPushButton(tr("Hints"));
130   buttons->addWidget(mySarhintsButton);
131   connect(mySarhintsButton, SIGNAL(clicked()), SLOT(showSarHints()));
132 
133   buttons->addStretch();
134 
135   mySarsaveButton = new QPushButton(tr("Save"));
136   buttons->addWidget(mySarsaveButton);
137   connect(mySarsaveButton, SIGNAL(clicked()), SLOT(saveSar()));
138 
139   myDefRespMsgLayout->addLayout(buttons, 3, 0, 1, 3);
140 
141   myDefRespMsgLayout->setColumnStretch(2, 1);
142 
143   sarGroupChanged(SarManager::AwayList);
144 
145 
146   QWidget* w = new QWidget(parent);
147   QVBoxLayout* mainLayout = new QVBoxLayout(w);
148   mainLayout->setContentsMargins(0, 0, 0, 0);
149   mainLayout->addWidget(myAutoAwayBox);
150   mainLayout->addWidget(myDefRespMsgBox);
151   return w;
152 }
153 
buildAutoStatusCombos(bool firstTime)154 void Settings::Status::buildAutoStatusCombos(bool firstTime)
155 {
156   int selectedNA, selectedAway;
157 
158   //Save selection (or get first selection)
159   if (firstTime)
160   {
161     selectedAway = Config::General::instance()->autoAwayMess();
162     selectedNA   = Config::General::instance()->autoNaMess();
163   }
164   else
165   {
166     selectedAway = myAutoAwayMessCombo->currentIndex();
167     selectedNA   = myAutoNaMessCombo->currentIndex();
168   }
169 
170   myAutoAwayMessCombo->clear();
171   myAutoAwayMessCombo->addItem(tr("Previous Message"),0);
172   const Licq::SarList& sarsAway(gSarManager.getList(SarManager::AwayList));
173   int count = 0;
174   for (Licq::SarList::const_iterator i = sarsAway.begin(); i != sarsAway.end(); ++i)
175     myAutoAwayMessCombo->addItem(QString::fromLocal8Bit(i->name.c_str()), ++count);
176   gSarManager.releaseList();
177 
178   myAutoNaMessCombo->clear();
179   myAutoNaMessCombo->addItem(tr("Previous Message"),0);
180   const Licq::SarList& sarsNa(gSarManager.getList(SarManager::NotAvailableList));
181   count = 0;
182   for (Licq::SarList::const_iterator i = sarsNa.begin(); i != sarsNa.end(); ++i)
183     myAutoNaMessCombo->addItem(QString::fromLocal8Bit(i->name.c_str()), ++count);
184   gSarManager.releaseList();
185 
186   myAutoAwayMessCombo->setCurrentIndex(selectedAway);
187   myAutoNaMessCombo->setCurrentIndex(selectedNA);
188 }
189 
sarMsgChanged(int msg)190 void Settings::Status::sarMsgChanged(int msg)
191 {
192   if (msg < 0)
193     return;
194 
195   const Licq::SarList& sars(gSarManager.getList(static_cast<SarManager::List>(mySarGroupCombo->currentIndex())));
196   mySartextEdit->setText(QString::fromLocal8Bit(sars[msg].text.c_str()));
197   gSarManager.releaseList();
198 }
199 
sarGroupChanged(int group)200 void Settings::Status::sarGroupChanged(int group)
201 {
202   if (group < 0)
203     return;
204 
205   mySarMsgCombo->clear();
206   const Licq::SarList& sars(gSarManager.getList(static_cast<SarManager::List>(group)));
207   for (Licq::SarList::const_iterator i = sars.begin(); i != sars.end(); ++i)
208     mySarMsgCombo->addItem(QString::fromLocal8Bit(i->name.c_str()));
209   gSarManager.releaseList();
210 
211   sarMsgChanged(0);
212 }
213 
saveSar()214 void Settings::Status::saveSar()
215 {
216   Licq::SarList& sars(gSarManager.getList(static_cast<SarManager::List>(mySarGroupCombo->currentIndex())));
217   Licq::SavedAutoResponse& sar(sars[mySarMsgCombo->currentIndex()]);
218   sar.name = mySarMsgCombo->currentText().toLocal8Bit().constData();
219   sar.text = mySartextEdit->toPlainText().toLocal8Bit().constData();
220   gSarManager.releaseList(true);
221 
222   buildAutoStatusCombos(0);
223 }
224 
showSarHints()225 void Settings::Status::showSarHints()
226 {
227   gMainWindow->showAutoResponseHints(dynamic_cast<QWidget*>(parent()));
228 }
229 
load()230 void Settings::Status::load()
231 {
232   Config::General* generalConfig = Config::General::instance();
233 
234   myAutoAwaySpin->setValue(generalConfig->autoAwayTime());
235   myAutoNaSpin->setValue(generalConfig->autoNaTime());
236   myAutoOfflineSpin->setValue(generalConfig->autoOfflineTime());
237   myAutoAwayMessCombo->setCurrentIndex(generalConfig->autoAwayMess());
238   myAutoNaMessCombo->setCurrentIndex(generalConfig->autoNaMess());
239 }
240 
apply()241 void Settings::Status::apply()
242 {
243   Config::General* generalConfig = Config::General::instance();
244   generalConfig->blockUpdates(true);
245 
246   generalConfig->setAutoAwayTime(myAutoAwaySpin->value());
247   generalConfig->setAutoNaTime(myAutoNaSpin->value());
248   generalConfig->setAutoOfflineTime(myAutoOfflineSpin->value());
249 
250   generalConfig->setAutoAwayMess(myAutoAwayMessCombo->currentIndex());
251   generalConfig->setAutoNaMess(myAutoNaMessCombo->currentIndex());
252 
253   generalConfig->blockUpdates(false);
254 }
255