N9profile
[n9profile] / mainwindow.cpp
1
2 #include "mainwindow.h"
3 #include "ui_mainwindow.h"
4 #include "profildialog.h"
5 #include "controlclass.h"
6 #include "profil.h"
7 #include "profiledb.h"
8 #include <QtCore/QDebug> //Debug pro informace
9 #include <QtGui/QMessageBox>
10 #include <QtGui/QListWidgetItem>
11 #include <QtGui/QCloseEvent>
12 #include <QMaemo5ValueButton>
13 #include <QMaemo5ListPickSelector>
14 #include <QMaemo5InformationBox>
15 #include <QtCore/QStringList>
16 #include <QtGui/QStandardItemModel>
17 #include "timedprofildialog.h"
18 #include <QtDBus/QDBusInterface>
19 const char name[] = "N9Profile";
20 /** Constructor.
21 Setup ui and create new object for item model and maemo5
22 value button
23 */
24 MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) :
25         QMainWindow(parent, flags),
26         ui(new Ui::MainWindow),
27         p_cnt_class(new ControlClass(this)),
28         p_set_Profile_button(NULL),
29         p_list_pick_profile(NULL),
30         p_model_profile_names(NULL),
31         p_Timed_Profile_button(NULL),
32         p_Calendar_Profile_button(NULL),
33         connectionInterface(new QDBusInterface("org.freedesktop.Notifications",
34                                                "/org/freedesktop/Notifications",
35                                                "org.freedesktop.Notifications",
36                                                QDBusConnection::systemBus(),this))
37 {
38     ui->setupUi(this);
39     createMermoButtons();
40 }
41
42
43 void MainWindow::sendNotif(QString name_of_profile)
44 {
45     if(isHidden()) {
46         qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "profil pro zlutou";
47         if (!connectionInterface->isValid()) {
48             qWarning() << "Cannotct conect to D-BUS";
49         } else {
50             //send message
51             qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "zobrazi ho ?";
52             connectionInterface->call(QLatin1String("SystemNoteInfoprint"),QVariant(name_of_profile));
53         }
54     }
55     ui->statusBar->showMessage(name_of_profile);
56 }
57
58 /** createMermoButtons()
59 Create maemo 5 buttons and model
60 */
61 void MainWindow::createMermoButtons()
62 {
63     p_model_profile_names = new QStandardItemModel(this);//model for maemo 5 button
64
65     p_set_Profile_button = new QMaemo5ValueButton(tr("Default  profile"), this);
66     p_set_Profile_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
67     p_list_pick_profile = new QMaemo5ListPickSelector();
68     p_list_pick_profile->setModel(p_model_profile_names);
69     p_set_Profile_button->setPickSelector(p_list_pick_profile);
70     ui->verticalLayout_2->addWidget(p_set_Profile_button);
71
72     //connect signal when user select profil to slot
73     connect(p_list_pick_profile,SIGNAL(selected(QString)),this,SLOT(SetDeafaultProfile(QString)));
74
75     p_Timed_Profile_button = new QMaemo5ValueButton(tr("Timed profile"), this);
76     p_Timed_Profile_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
77     //when user click on Timed profile button
78     connect(p_Timed_Profile_button,SIGNAL(clicked()),this,SLOT(SetTimedProfile()));
79
80
81     p_Calendar_Profile_button = new QMaemo5ValueButton(tr("Calendar profiles"), this);
82     p_Calendar_Profile_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
83     connect(p_Calendar_Profile_button,SIGNAL(clicked()),this, SLOT(SetCalendarProfiles()));
84     ui->verticalLayout_2->addWidget(p_Calendar_Profile_button);
85
86
87     p_IDWifi_Profile_button = new QMaemo5ValueButton(tr("Network profile"), this);
88     p_IDWifi_Profile_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
89     connect(p_IDWifi_Profile_button,SIGNAL(clicked()),this, SLOT(SetNetworkProfile()));
90     ui->verticalLayout_2->addWidget(p_IDWifi_Profile_button);
91
92     ui->verticalLayout_2->addWidget(p_Timed_Profile_button);
93 }
94
95 /** Destructor.
96 Delete the UI and the control class
97 */
98 MainWindow::~MainWindow()
99 {
100     delete ui;
101     delete p_cnt_class;
102 }
103
104 /** Init.
105 Used to create and initialize the control class.
106 */
107 void MainWindow::Init()
108 {
109
110     //connec signal when user sets timed profile or when timed profile ends
111     connect(this,SIGNAL(s_hide_show()),p_cnt_class,SLOT(ShowHide()));
112     connect(p_cnt_class,SIGNAL(s_timed_profile_name(QString)),this,SLOT(SetTimedProfilename(QString)));
113     connect(p_cnt_class,SIGNAL(s_calendar_profile_name(QString)),this,SLOT(SetCalendarProfilename(QString)));
114     connect(p_cnt_class,SIGNAL(s_network_profile_name(QString)),this,SLOT(SetMetworkProfilename(QString)));
115     connect(p_cnt_class,SIGNAL(s_profile_now(QString)),this,SLOT(sendNotif(QString)));
116
117     connect(this,SIGNAL(s_aboutToEnd()),p_cnt_class->GetProfileDB(),SLOT(SaveProfilesToXml())); //save xml berofe end
118     //signal when profiles are changed (add profile, delete profile)
119     connect(p_cnt_class->GetProfileDB()->GetProfileManager(),SIGNAL(s_profiles_name_change(QStringList)),this,SLOT(changeInProfiles(QStringList)));
120     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "init controll class";
121     p_cnt_class->Init(&settings);
122
123     // changeInProfiles(p_cnt_class->GetProfileDB()->GetProfilesNames());//get profiles names and sets them
124 }
125
126 /** changeInProfiles();.
127 Slot for signal when new profile is created or deleted
128 \param Profiles_Names list of profile names
129 */
130 void MainWindow::changeInProfiles(QStringList Profiles_Names)
131 {
132     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "set profile names to listwidgetview ";
133     ui->listWidgetProfiles->clear();//clear view
134     ui->listWidgetProfiles->addItems(Profiles_Names);//set names
135
136     p_model_profile_names->clear();//clear model for maemo buttons
137
138     for (int i = 0; i < Profiles_Names.size(); ++i){ //add
139         p_model_profile_names->appendRow(new QStandardItem(Profiles_Names.at(i)));
140         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "pridavam ted: " << Profiles_Names.at(i);
141     }
142     setNewIndexDeafaultProfile();
143 }
144
145
146 /** setNewIndexDeafaultProfile();.
147 Method for setting index in list pick
148 */
149 void MainWindow::setNewIndexDeafaultProfile()
150 {
151     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "select index";
152     QList<QStandardItem *> list_of_items;
153     //get defult name,MatchExactly and find in model
154     list_of_items = p_model_profile_names->findItems(p_cnt_class->GetDefaultProfile(), Qt::MatchExactly, 0);//find in 1.
155     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "size of list is: " << list_of_items.count();
156     if(!list_of_items.empty()){
157         //set list pick index
158         p_list_pick_profile->setCurrentIndex(list_of_items.at(0)->row());
159         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "item in list pick selected:" ;
160     }
161 }
162
163 /** on_listWidgetProfiles_itemClicked
164 Slot for  signal when user click on item in QListWidget
165 and show dialog to update profile
166 \param item item in list widget
167 */
168 void MainWindow::on_listWidgetProfiles_itemClicked(QListWidgetItem* item)
169 {
170     Profil * profil;
171     ProfileDB* db =  p_cnt_class->GetProfileDB();// get profile DB;
172     QMessageBox msgBox;
173     msgBox.setIcon(QMessageBox::Information);
174     msgBox.setText(tr("Delete or modify profile?"));
175     msgBox.setEscapeButton(QMessageBox::Cancel);
176     QPushButton *CancelButton =  msgBox.addButton(QMessageBox::Cancel);
177     QPushButton *DeleteButton = msgBox.addButton(tr("Delete"), QMessageBox::ActionRole);
178     QPushButton *modifyButton = msgBox.addButton(tr("Modify"),QMessageBox::DestructiveRole);
179
180
181     msgBox.exec();
182     if (msgBox.clickedButton() == DeleteButton) {
183         if(item->text() == p_list_pick_profile->currentValueText()){
184             QMaemo5InformationBox::information(this, tr("You cannot delete deafult profile"), QMaemo5InformationBox::DefaultTimeout);
185         }else {
186             //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "bude se namaaz";
187             db->GetProfileManager()->DeleteProfile(item->text());
188         }
189     } else if (msgBox.clickedButton() == modifyButton) {
190         profil = new Profil(item->text()); //create profil and sets name
191
192         if(!db->GetProfileManager()->GetProfile(profil))//
193         {
194             return;
195         }
196
197         ProfilDialog dialog(db->GetProfileManager()->GetProfilesNames(),this, profil,db->GetSoundFilesManager()->GetModel()); //
198
199         int result = dialog.exec();
200
201         if(result)
202         {
203             //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "OK update profile";
204
205             db->GetProfileManager()->UpdateProfile(profil);
206         }
207         delete profil;
208     }else if (msgBox.clickedButton() == CancelButton) {
209         return;
210     }
211 }
212
213 /** on_ButtonNewProfile_clicked();.
214 Serves as a slot for the button to create a new profile.
215 Creates a new profile and forward links.
216 */
217 void MainWindow::on_ButtonNewProfile_clicked()
218 {
219     ProfileDB* db =   p_cnt_class->GetProfileDB();
220     Profil * profil = new Profil("");//new profiel
221     ProfilDialog dialog(db->GetProfileManager()->GetProfilesNames(),this, profil,db->GetSoundFilesManager()->GetModel()); //dostane profil, jmena profilu a model
222
223     int result = dialog.exec();
224
225     if(result)
226     {
227         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "save profile";
228         db->GetProfileManager()->CreateProfile(profil);
229     }
230     delete profil;
231 }
232
233 /** SetTimedProfile
234 Slot for  signal when user click on button for timed profile
235 */
236 void MainWindow::SetTimedProfile()
237 {
238     p_cnt_class->SetTimeProfile();
239 }
240
241 /** SetTimedProfilename
242 Slot for  signal when set profile
243 \param profile_name name of profile
244 */
245 void MainWindow::SetTimedProfilename(QString profile_name)
246 {
247     p_Timed_Profile_button->setValueText(profile_name);
248 }
249
250 /** SetCalendarProfiles
251 Slot for  signal when user click on button for calendar profile
252 */
253 void MainWindow::SetCalendarProfiles()
254 {
255     p_cnt_class->SetCalendar();
256 }
257
258 /** SetNetworkProfile
259 Slot for  signal when user click on button for network profile
260 */
261 void MainWindow::SetNetworkProfile()
262 {
263     p_cnt_class->SetNetwork();
264     //=======================================
265 }
266
267 /** SetCalendarProfilename
268 Slot for  signal when calendar object select profile
269 \param profile_name name of profile
270 */
271 void MainWindow::SetCalendarProfilename(QString profile_name)
272 {
273     p_Calendar_Profile_button->setValueText(profile_name);
274 }
275
276 /** SetDeafaultProfile(QString profile_name)
277 Slot for  signal when user choose profile name from QMaemoButton
278 \param profile_name name of profile
279 */
280 void MainWindow::SetDeafaultProfile(QString profile_name)
281 {
282     p_cnt_class->SetDefaultProfile(profile_name);
283 }
284
285 /** SetMetworkProfilename(QString profile_name)
286 Slot for  signal when network object select profile
287 \param profile_name name of profile
288 */
289 void MainWindow::SetMetworkProfilename(QString profile_name )
290 {
291     p_IDWifi_Profile_button->setValueText(profile_name);
292 }
293
294 //======================================================================================
295 /** closeEvent(QCloseEvent *event)
296 Slot for close event.
297 It will call method maybeQuit()
298 */
299 void MainWindow::closeEvent(QCloseEvent *event)
300 {
301     hide();
302     event->ignore();
303     emit s_hide_show();
304 }
305
306 void MainWindow::on_actionExit_triggered()
307 {
308     if (maybeQuit()) {
309         emit s_aboutToEnd();
310         QCoreApplication::quit();
311     } else {
312         return;
313     }
314 }
315
316 /** maybeQuit()
317 Method for show QMessageBox before
318 It will ask user if he want to quit.
319 */
320 bool MainWindow::maybeQuit()
321 {
322     QMessageBox::StandardButton ret;
323     ret = QMessageBox::warning(this, tr("Application"),
324                                tr("Program will end.\n"
325                                   "Do you want to quit?"),
326                                QMessageBox::Ok | QMessageBox::Cancel);
327     switch (ret) {
328     case QMessageBox::Ok:
329         return true;
330         break;
331     case QMessageBox::Cancel:
332         return false;
333         break;
334     default:
335         return false;
336     }
337     return true;
338 }
339
340 int MainWindow::top_application()
341 {
342     show();
343     return 0;
344 }
345
346
347 //================================================================================
348 /** \brief changeEvent
349  *
350  *
351  *
352  */
353 void MainWindow::changeEvent(QEvent *e)
354 {
355     QMainWindow::changeEvent(e);
356     switch (e->type()) {
357     case QEvent::LanguageChange:
358         ui->retranslateUi(this);
359         break;
360     default:
361         break;
362     }
363 }
364
365 /** \brief on_actionAboutQT_triggered
366  *
367  *
368  *
369  */
370 void MainWindow::on_actionAboutQT_triggered()
371 {
372     QMessageBox::aboutQt(this);
373 }
374
375 /** \brief on_actionAbout_triggered
376  *
377  *
378  *
379  */
380 void MainWindow::on_actionAbout_triggered()
381 {
382     QString str = QString("");
383     str.append(tr("This program can create mobile profiles and set them. "));
384     str.append(tr("Program will change only general profil in N900. "));
385     str.append(tr("When you first start N9Profil it will create two profile from N900, general a silent. "));
386     str.append(tr("You can set default profile, timed profile for "));
387     str.append(tr("period of time or you can set profiles based on desctriptions "));
388     str.append(tr("in events in calendar. "));
389     str.append(tr("Timed profile have bigger priority, then calendar and calendar then default profile. "));
390     QMessageBox::about(this,tr("About N9Profil"), str);
391 }
392
393 void MainWindow::on_pushButtonPhonenum_clicked()
394 {
395     p_cnt_class->SetTelNum();
396 }