README
[n9profile] / networkprofile.cpp
1 #include "networkprofile.h"
2 #include <QtCore/QStringList>
3 #include <QtCore/QSettings>
4 #include <QtCore/QDebug> //Debug pro informace
5 #include <QtGui/QStandardItemModel>
6 #include <QtGui/QItemSelectionModel>
7 #include <QtCore/QTimer>
8 #include "networkdialogdialog.h"
9
10 using namespace QtMobility;
11 /** Constructor.
12   Get setting pointer and create network class.
13   Connect all signal from networkclass to slot
14   and gets name of network
15 */
16 NetWorkProfile::NetWorkProfile(QWidget *parent, QSettings *sett) :
17         QWidget(parent)
18 {
19     settings = sett;
20     p_networkcl = new QSystemNetworkInfo(this);
21     timer = new QTimer(this);
22     connect(timer,SIGNAL(timeout()),this,SLOT(ChangeInCellLocation()));
23     timer->start(10000);//each 10 seconds shot
24     connect(p_networkcl,SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)),this,SLOT(ChangeInWifiName(QSystemNetworkInfo::NetworkMode,  QString )));
25     qDebug()<< "ziskam id============================";
26     CellLocation = QString::number(p_networkcl->locationAreaCode());
27     qDebug()<< "==================" << CellLocation;
28     WifiName = p_networkcl->networkName(QSystemNetworkInfo::WlanMode);
29     qDebug()<< "==================" << WifiName;
30     model_of_CellId = NULL;
31     model_of_WifiIf = NULL;
32 }
33
34
35 /** SetProfilenames.
36 Set profiles names
37 \param list_of_profiles_nf names of profiles
38 */
39 void NetWorkProfile::SetProfilenames(QStringList list_of_profiles_nf)
40 {
41     list_of_profiles = list_of_profiles_nf;
42 }
43
44 /** DeletedProfile.
45 Slot for name of deleted profile
46 \param list_of_profiles_nf names of profiles
47 */
48 void NetWorkProfile::DeletedProfile(QString name)
49 {
50     if(name == profile_Currently)
51         SetProfile();
52 }
53
54 /** createModels
55 gets model and create selection models
56 */
57 void NetWorkProfile::createModels(QStandardItemModel * cellM, QStandardItemModel * wifiM)
58 {
59     if((cellM == NULL) || (wifiM == NULL)){
60      //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "jeden je null" ;
61  }
62     model_of_CellId =  cellM;
63     selModel_CellId = new QItemSelectionModel(model_of_CellId);
64     model_of_WifiIf = wifiM;
65     selModel_WifiIf = new QItemSelectionModel(model_of_WifiIf);
66     SetProfile();;//set profile
67 }
68
69
70 /** ChangeInCellLocation.
71 Slot for change in location
72 \param list_of_profiles_nf names of profiles
73 */
74 void NetWorkProfile::ChangeInCellLocation()
75 {
76     CellLocation = QString::number(p_networkcl->locationAreaCode());
77      WifiName = p_networkcl->networkName(QSystemNetworkInfo::WlanMode);
78          qDebug()<< "=====změna v id lov" << CellLocation;
79     SetProfile();
80 }
81
82 /** ChangeInWifiName.
83 Slot for change in wifi name
84 \param list_of_profiles_nf names of profiles
85 */
86 void NetWorkProfile::ChangeInWifiName(QSystemNetworkInfo::NetworkMode mode,  QString name )
87 {
88     qDebug()<< "změna ve jménu" << name;
89
90     if(mode == QSystemNetworkInfo::WlanMode)
91     {
92         qDebug()<< "změna ve jménu wifi" << name;
93         WifiName = name;
94         SetProfile();
95     }
96 }
97
98 /** SetProfile.
99 Set profile based of name of wifi or location id
100 Find rule in tables and set profile
101 */
102 void NetWorkProfile::SetProfile()
103 {
104     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "kontrola zda jsou profily null";
105     if((model_of_CellId == NULL) || (model_of_WifiIf == NULL)) return;
106     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "bude se ted nastavovoat nový profil";
107     profile_Currently = QString("");
108     QString str = QString("");
109
110     if(!WifiName.isEmpty())
111     {
112         // profile_Currently
113         str = FindWifiNameInModel(WifiName);
114         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "nalezlo to profil pro wifi sit?  " << str;
115         if(str.isEmpty())
116         {
117             //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "";
118             str = FindIDLocInModel(CellLocation);
119             profile_Currently = str;
120             //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "profil na ic loc " << profile_Currently;
121             emit s_network_profile(profile_Currently);
122             return;
123         } else {
124             profile_Currently = str;
125             //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "profil na wifi name " << profile_Currently;
126             emit s_network_profile(profile_Currently);
127             return;
128         }
129     } else
130     {
131         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "wifi neni   " ;
132         str = FindIDLocInModel(CellLocation);
133         profile_Currently = str;
134         emit s_network_profile(profile_Currently);
135         return;
136     }
137     emit s_network_profile(profile_Currently);
138 }
139
140 /** FindIDLocInModel.
141 Find id location and return name of profile
142 \param IDCell location id
143 */
144 QString NetWorkProfile::FindIDLocInModel(QString IDCell)
145 {
146     QString name = QString("");
147     QList<QStandardItem *> list_of_idcell;
148
149     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "Bude hledat modelu wifi site " ;
150
151     //labels << tr("Name") << tr("Wifi name") << tr("Profile");
152     list_of_idcell = model_of_CellId->findItems(IDCell, Qt::MatchExactly , 1);
153
154     if(list_of_idcell.empty())
155     {
156         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "nic nanasel " ;
157         return name;
158     } else
159     {
160         name = model_of_CellId->item(list_of_idcell.at(0)->row(),2)->data(Qt::DisplayRole).toString();
161         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "nasel ho  " << name;
162         return name;
163     }
164 }
165
166 /** FindWifiNameInModel.
167 Find profile in table based in wifiname
168 \param WifiNam name of wifi
169 */
170 QString NetWorkProfile::FindWifiNameInModel(QString WifiNam)
171 {
172     QString name = QString("");
173     QList<QStandardItem *> list_of_wifiname;
174
175     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "Bude hledat modelu wifi site " ;
176     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "pocet radku  v modelu wifi " << model_of_WifiIf->rowCount();
177     //labels << tr("Name") << tr("Wifi name") << tr("Profile");
178     list_of_wifiname = model_of_WifiIf->findItems(WifiNam, Qt::MatchExactly , 1); //fing wifi name in 1 colum
179     if(list_of_wifiname.empty())
180     {
181         return name;
182     } else
183     {
184         name = model_of_WifiIf->item(list_of_wifiname.at(0)->row(),2)->data(Qt::DisplayRole).toString();
185         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "nasel ho  " << name;
186         return name;
187     }
188 }
189
190 //===========================================================================
191 /** ShowDialog
192 For dialog for modifi calendars and profiles
193 */
194 void NetWorkProfile::ShowDialog()
195 {
196     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "dialog " ;
197     NetWorkdialogDialog dialog(this, settings);
198         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "ma setings " ;
199     dialog.SetViews(model_of_CellId,selModel_CellId,model_of_WifiIf,selModel_WifiIf);
200         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "modely " ;
201     dialog.SetIDWIFInames(CellLocation, WifiName);
202         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "lokaci a wifi " ;
203     dialog.SetProfilenames(list_of_profiles);
204         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "prily " ;
205     connect(&dialog,SIGNAL(s_change_model()),this,SLOT(ChangeInModel()));
206     dialog.exec();
207         //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "konec " ;
208     disconnect(&dialog,SIGNAL(s_change_model()),this,SLOT(ChangeInModel()));
209 }
210
211 /** ChangeInModel.
212 Slot for change in model table
213 */
214 void NetWorkProfile::ChangeInModel()
215 {
216     SetProfile();
217 }
218