README
[n9profile] / networkdialogdialog.cpp
1 #include "networkdialogdialog.h"
2 #include "ui_networkdialogdialog.h"
3 #include <QtCore/QStringList>
4 #include <QtCore/QDebug> //Debug pro informace
5 #include <QtGui/QItemSelectionModel>
6 #include <QtGui/QStandardItemModel>
7 #include <QMaemo5InformationBox>
8 #include <QMaemo5ValueButton>
9 #include <QMaemo5ListPickSelector>
10 #include <QMaemo5InformationBox>
11 #include <QtGui/QMessageBox>
12 #include "networkdialogedit.h"
13
14 /** Constructor.
15   Gets pointer to settings and set window title
16 */
17 NetWorkdialogDialog::NetWorkdialogDialog(QWidget *parent, QSettings *sett) :
18         QDialog(parent),
19         ui(new Ui::NetWorkdialogDialog)
20 {
21     ui->setupUi(this);
22     settings = sett;
23     setWindowTitle(tr("Network profile"));
24 }
25
26 /** Destructor.
27    Delete ui
28 */
29 NetWorkdialogDialog::~NetWorkdialogDialog()
30 {
31     delete ui;
32 }
33
34 void NetWorkdialogDialog::changeEvent(QEvent *e)
35 {
36     QDialog::changeEvent(e);
37     switch (e->type()) {
38     case QEvent::LanguageChange:
39         ui->retranslateUi(this);
40         break;
41     default:
42         break;
43     }
44 }
45
46
47 /** SetViews.
48 Set view models, behavior, mode and triggerrs
49 \param model_of_profiles model of profiles
50 \param selModel_profiles selection model for profiles
51 \param model_of_calendars model of calendars
52 \param selModel_calendars selection model for calendars
53 */
54 void NetWorkdialogDialog::SetViews(QStandardItemModel *model_of_CellId ,
55                                    QItemSelectionModel *selModel_CellId,
56                                    QStandardItemModel *model_of_WifiIf ,
57                                    QItemSelectionModel *selModel_WifiIf)
58 {
59     ui->tableViewLocationId->setModel(model_of_CellId);
60
61     ui->tableViewLocationId->setSelectionModel(selModel_CellId);
62     ui->tableViewLocationId->setSelectionBehavior(QAbstractItemView::SelectRows);
63     ui->tableViewLocationId->setSelectionMode(QAbstractItemView::SingleSelection);
64     ui->tableViewLocationId->setEditTriggers(QAbstractItemView::NoEditTriggers);
65     //ui->tableView_2->resizeColumnsToContents();
66     ui->tableViewLocationId->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
67     //labels << tr("Name") << tr("Wifi name") << tr("Profile");
68     ui->tableView_WIFIID->setModel(model_of_WifiIf);
69     ui->tableView_WIFIID->setSelectionModel(selModel_WifiIf);
70     ui->tableView_WIFIID->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
71     ui->tableView_WIFIID->setSelectionBehavior(QAbstractItemView::SelectRows);
72     ui->tableView_WIFIID->setSelectionMode(QAbstractItemView::SingleSelection);
73     ui->tableView_WIFIID->setEditTriggers(QAbstractItemView::NoEditTriggers);
74 }
75
76 /** SetProfilenames.
77 Set profiles names
78 \param list_of_profiles_nf names of profiles
79 */
80 void NetWorkdialogDialog::SetProfilenames(QStringList list_of_profiles_nf)
81 {
82     list_of_profiles = list_of_profiles_nf;
83
84     ui->buttonGroup->setId(ui->radioButtonIDLoc,0);
85     ui->buttonGroup->setId(ui->radioButtonWifi,1);
86
87     ui->radioButtonIDLoc->setChecked(true);
88
89 }
90
91 /** SetIDWIFInames.
92 Set text for labels
93 \param idloc location id
94 \param wifiname wifi name
95 */
96 void NetWorkdialogDialog::SetIDWIFInames(QString idloc, QString wifiname)
97 {
98     if(idloc == "-1")
99     {
100         ui->labelIDloc->setText(tr("Phone not connected"));
101         ui->labelIDLoacName->setText("");
102     } else {
103         ui->labelIDLoacName->setText(idloc);
104     }
105
106     if(wifiname.isEmpty())
107     {
108         ui->labelWifi->setText(tr("Wifi not connected"));
109         ui->labelWifiname->setText(wifiname);
110     } else {
111         ui->labelWifiname->setText(wifiname);
112     }
113 }
114
115
116 /** createNewRule()
117 Create new rule for location or wifi
118 */
119 void NetWorkdialogDialog::createNewRule()
120 {
121     int id = ui->buttonGroup->checkedId();
122     //check is mobile connected
123     if((ui->labelWifiname->text().isEmpty()) && (id == 1))
124     {
125         QMaemo5InformationBox::information(this, tr("Not connedted to wifi"), QMaemo5InformationBox::DefaultTimeout);
126         return;
127     }
128
129     if((ui->labelIDLoacName->text().isEmpty())&& (id == 0) )
130     {
131         QMaemo5InformationBox::information(this, tr("Mobile not connected"), QMaemo5InformationBox::DefaultTimeout);
132         return;
133     }
134
135     if((id == 1) && (FindWifiNameInModel(ui->labelWifiname->text())))
136     {
137         QMaemo5InformationBox::information(this, tr("This rule for wifi exists"), QMaemo5InformationBox::DefaultTimeout);
138         return;
139     }
140
141     if((id == 0) && (FindIDLocInModel(ui->labelIDLoacName->text())))
142     {
143         QMaemo5InformationBox::information(this, tr("This rule for location exists"), QMaemo5InformationBox::DefaultTimeout);
144         return;
145     }
146
147
148     QStandardItemModel *model;
149     QList<QStandardItem *> listitems;
150
151     NetWorkDialogEdit dialog(this);
152     if(id == 0)//for location
153     {
154         dialog.SetNameOfLabel(ui->labelIDLoacName->text(), QString(tr("New location rule for: ")));
155         model = (QStandardItemModel *) ui->tableViewLocationId->model();//model je ted id loc
156         listitems = model->findItems(ui->labelIDLoacName->text());
157         if(listitems.empty())//nic nanašel
158         {
159             dialog.SetProfilenames(list_of_profiles); //dam tomu seznan pro výběr
160             int result = dialog.exec();
161             if(result)
162             {
163                 //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "Nove providloooooooooo";
164                 //nove jmeno pro pravidlo
165                 listitems.append(new QStandardItem(dialog.GetNameOfRule()));//jmeno pravidla
166                 listitems.append(new QStandardItem(ui->labelIDLoacName->text()));//nova lokace
167                 listitems.append(new QStandardItem(dialog.GetNameProfile()));//jmeno profilu
168                 model->appendRow(listitems);
169                 emit s_change_model();//vyslu zmenu
170                 //labels << tr("Name") << tr("Wifi name") << tr("Profile");davam tam id lokace prvna
171             }
172         }
173         else // neco našel a to je špatně
174         {
175             QMaemo5InformationBox::information(this, tr("This rule exist"), QMaemo5InformationBox::DefaultTimeout);
176         }
177
178     } else if(id == 1)//for wifi
179     {
180         dialog.SetNameOfLabel(ui->labelWifiname->text(), QString(tr("New WiFi rule for: ")));
181         model = (QStandardItemModel *) ui->tableView_WIFIID->model(); //model pro wifi
182         listitems = model->findItems(ui->labelWifiname->text());
183         if(listitems.empty())//nic nanašel
184         {
185             dialog.SetProfilenames(list_of_profiles); //dam tomu seznan pro výběr
186             int result = dialog.exec();
187             if(result)
188             {
189                 //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "Nove providloooooooooo";
190                 //nove jmeno pro pravidlo
191                 listitems.append(new QStandardItem(dialog.GetNameOfRule()));//jmeno pravidla
192                 listitems.append(new QStandardItem(ui->labelWifiname->text()));//nove wifi
193                 listitems.append(new QStandardItem(dialog.GetNameProfile()));//jmeno profilu
194                 model->appendRow(listitems);
195                 emit s_change_model();//vyslu zmenu
196                 //labels << tr("Name") << tr("Wifi name") << tr("Profile");davam tam id lokace prvna
197             }
198         }
199         else // neco našel a to je špatně
200         {
201             QMaemo5InformationBox::information(this, tr("This rule exist"), QMaemo5InformationBox::DefaultTimeout);
202         }
203     }
204 }
205
206 /** on_tableViewLocationId_clicked()
207 Slot when user click on location
208 user can modifi or delete rule
209 */
210 void NetWorkdialogDialog::on_tableViewLocationId_clicked(QModelIndex index)
211 {
212     Q_UNUSED(index)
213     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "kliknutu na idloc";
214
215     QItemSelectionModel *selModel = ui->tableViewLocationId->selectionModel();
216     if(!selModel->hasSelection()) return;//no selection retirn
217     QModelIndexList list_of_selected = selModel->selectedIndexes(); //get selected
218     QStandardItemModel *model = (QStandardItemModel *) ui->tableViewLocationId->model(); //model
219     int row = list_of_selected.at(0).row();//get row
220
221     QMessageBox msgBox;
222     msgBox.setIcon(QMessageBox::Information);
223     msgBox.setText(tr("Delete or modify profile?"));
224     msgBox.setEscapeButton(QMessageBox::Cancel);
225     QPushButton *CancelButton =  msgBox.addButton(QMessageBox::Cancel);
226     QPushButton *DeleteButton = msgBox.addButton(tr("Delete"), QMessageBox::ActionRole);
227     QPushButton *modifyButton = msgBox.addButton(tr("Modify"),QMessageBox::DestructiveRole);
228
229     msgBox.exec(); //delete or modifi
230     if (msgBox.clickedButton() == DeleteButton) {
231
232         //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "bude se mazat pravidlo";
233         model->removeRow(row);
234         selModel->clearSelection();
235         emit s_change_model(); //vyslu zmenu
236     }else if (msgBox.clickedButton() == modifyButton) {
237         NetWorkDialogEdit dialog(this); //modifi
238         //labels << tr("Name") << tr("Wifi name") << tr("Profile");davam tam id lokace prvna
239         dialog.SetNameOfLabel(list_of_selected.at(1).data().toString(), QString(tr("Change profile for location: ")));
240         dialog.SetProfilenames(list_of_profiles); //dam tomu seznan pro výběr
241         dialog.SetNameOfRule(list_of_selected.at(0).data().toString()); //dam tomu jmeno pravidla
242
243         int result = dialog.exec();
244         if(result)
245         {
246             //nove jmeno pro pravidlo
247             model->item(list_of_selected.at(0).row(),list_of_selected.at(0).column())->setData(QVariant(dialog.GetNameOfRule()),Qt::DisplayRole);
248             //new name
249             model->item(list_of_selected.at(2).row(),list_of_selected.at(2).column())->setData(QVariant(dialog.GetNameProfile()),Qt::DisplayRole);
250
251             emit s_change_model(); //vyslu zmenu
252         }
253         selModel->clearSelection();
254     }else if (msgBox.clickedButton() == CancelButton) {
255         selModel->clearSelection();
256         return;
257     }
258 }
259
260 /** on_tableView_WIFIID_clicked()
261 Slot when user click on location
262 user can modifi or delete rule
263 */
264 void NetWorkdialogDialog::on_tableView_WIFIID_clicked(QModelIndex index)
265 {
266     Q_UNUSED(index)
267     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "jmeno wifi" ;
268     QItemSelectionModel *selModel = ui->tableView_WIFIID->selectionModel();
269     if(!selModel->hasSelection()) return;
270     QModelIndexList list_of_selected = selModel->selectedIndexes();
271     QStandardItemModel *model = (QStandardItemModel *) ui->tableView_WIFIID->model();
272     int row = list_of_selected.at(0).row();//get row
273     //======================================
274     //message box for choose
275     QMessageBox msgBox;
276     msgBox.setIcon(QMessageBox::Information);
277     msgBox.setText(tr("Delete or modify profile?"));
278     msgBox.setEscapeButton(QMessageBox::Cancel);
279     QPushButton *CancelButton =  msgBox.addButton(QMessageBox::Cancel);
280     QPushButton *DeleteButton = msgBox.addButton(tr("Delete"), QMessageBox::ActionRole);
281     QPushButton *modifyButton = msgBox.addButton(tr("Modify"),QMessageBox::DestructiveRole);
282
283     msgBox.exec();//======================================
284     if (msgBox.clickedButton() == DeleteButton) {
285
286         //qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "bude se mazat pravidlo";
287         model->removeRow(row);
288         selModel->clearSelection();
289         emit s_change_model(); //vyslu zmenu
290     }else if (msgBox.clickedButton() == modifyButton) { //show dialog for modifi
291         NetWorkDialogEdit dialog(this);
292         dialog.SetNameOfLabel(list_of_selected.at(1).data().toString(), QString(tr("Change profile for Wifi: ")));
293         dialog.SetProfilenames(list_of_profiles); //dam tomu seznan pro výběr
294         //labels << tr("Name") << tr("Wifi name") << tr("Profile");davam tam id lokace prvna
295         dialog.SetNameOfRule(list_of_selected.at(0).data().toString()); //dam tomu jmeno pravidla
296         int result = dialog.exec();
297
298         if(result)
299         {
300             ////qDebug() <<  __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "OK update profile";
301             //nove jmeno pro pravidlo
302             model->item(list_of_selected.at(0).row(),list_of_selected.at(0).column())->setData(QVariant(dialog.GetNameOfRule()),Qt::DisplayRole);
303             //new name
304             model->item(list_of_selected.at(2).row(),list_of_selected.at(2).column())->setData(QVariant(dialog.GetNameProfile()),Qt::DisplayRole);
305
306             emit s_change_model(); //vyslu zmenu
307         }
308         selModel->clearSelection();
309     }   else if (msgBox.clickedButton() == CancelButton) {
310         selModel->clearSelection();
311         return;
312     }
313 }
314
315 /** on_pushButtonNewrule_clicked()
316 Slot when user click on create new rule button
317 */
318 void NetWorkdialogDialog::on_pushButtonNewrule_clicked()
319 {
320     createNewRule();
321 }
322
323
324 /** FindIDLocInModel.
325 Find id location and return name of profile
326 \param IDCell location id
327 */
328 bool NetWorkdialogDialog::FindIDLocInModel(QString IDCell)
329 {
330     QList<QStandardItem *> list_of_idcell;
331     QStandardItemModel *model  ;
332      model = (QStandardItemModel *) ui->tableViewLocationId->model();//model je ted id loc
333
334     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "Bude hledat modelu wifi site " ;
335
336     //labels << tr("Name") << tr("Wifi name") << tr("Profile");
337     list_of_idcell = model->findItems(IDCell, Qt::MatchExactly , 1);
338
339     return !list_of_idcell.empty();
340 }
341
342 /** FindWifiNameInModel.
343 Find profile in table based in wifiname
344 \param WifiNam name of wifi
345 */
346 bool NetWorkdialogDialog::FindWifiNameInModel(QString WifiNam)
347 {
348     QList<QStandardItem *> list_of_wifiname;
349     QStandardItemModel *model;
350     model  = (QStandardItemModel *) ui->tableView_WIFIID->model(); //model pro wifi
351
352     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "Bude hledat modelu wifi site " ;
353     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "pocet radku  v modelu wifi " << model_of_WifiIf->rowCount();
354     //labels << tr("Name") << tr("Wifi name") << tr("Profile");
355     list_of_wifiname = model->findItems(WifiNam, Qt::MatchExactly , 1); //fing wifi name in 1 colum
356     return !list_of_wifiname.empty();
357
358 }
359