N9profile
[n9profile] / controlclass.cpp
1
2 #include <QtCore/QSettings>
3 #include "controlclass.h"
4 #include "profiledb.h"
5 #include <QtCore/QDebug> //Debug pro informace
6 #include "timeprofile.h"
7 #include "calendatprofile.h"
8 #include "networkprofile.h"
9 #include "telephonenumprofile.h"
10
11 /** Constructor.
12   Set pointers to NULL and create new profile db
13 */
14 ControlClass::ControlClass(QWidget *parent) :
15         QWidget(parent)
16 {
17     settings = NULL;
18     p_profile_db = new ProfileDB(this); //create new db
19 }
20
21 /** Destructor.
22 */
23 ControlClass::~ControlClass()
24 {
25 }
26
27 /** Init.
28   Used to initialize and connect slot and signals
29   \param setting reference to the setting class
30 */
31 void ControlClass::Init( QSettings *setting )
32 {
33     settings = setting;
34
35     p_time_profile = new TimeProfile(this,setting); //create class for setting timed profile
36     connect(p_profile_db->GetProfileManager(),SIGNAL(s_profiles_name_change(QStringList)),p_time_profile,SLOT(ChangeInProfles(QStringList)));
37     p_calendar_profile = new CalendarProfile(this, settings);
38     connect(p_profile_db->GetProfileManager(),SIGNAL(s_profile_update(QString)),this,SLOT(ChangeInProfile(QString)));
39     connect(p_profile_db->GetProfileManager(),SIGNAL(s_profiles_name_change(QStringList)),p_calendar_profile,SLOT(setProfiles(QStringList)));
40     connect(p_time_profile,SIGNAL(s_set_timed_profile(QString)),this,SLOT(ChangeTimedProfile(QString)));
41     connect(p_calendar_profile,SIGNAL(s_calendar_profile(QString)),this,SLOT(ChangeCalendarProfile(QString)));
42     p_network_profile = new NetWorkProfile(this,setting);
43     connect(p_network_profile,SIGNAL(s_network_profile(QString)),this,SLOT(ChangeNetworkProfile(QString)));
44     connect(p_profile_db->GetProfileManager(),SIGNAL(s_profiles_name_change(QStringList)),p_network_profile,SLOT(SetProfilenames(QStringList)));
45     connect(p_profile_db->GetProfileManager(),SIGNAL(s_profile_delete(QString)),p_network_profile,SLOT(DeletedProfile(QString)));
46     p_telenum_profil = new TelephoneNumProfile(this);
47     connect(p_profile_db->GetProfileManager(),SIGNAL(s_profiles_name_change(QStringList)),p_telenum_profil,SLOT(SetProfilenames(QStringList)));
48     connect(p_telenum_profil,SIGNAL(s_telnum_profile(QString)),this,SLOT(ChangeTelNumProfile(QString)));
49     qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "create new db";
50     //set deafult profile
51     settings->beginGroup("ControlClass");
52     default_profile = settings->value("DefaultProfile", "general").toString(); //set default profile
53     settings->endGroup();
54
55     //for signal when profile is changed (update profile)
56
57     p_profile_db->Init();//init db
58
59     p_calendar_profile->Init();
60     p_network_profile->createModels(p_profile_db->GetRulesManager()->GetCellIDModel(),p_profile_db->GetRulesManager()->GetWiFiModel());
61     p_telenum_profil->createModels(p_profile_db->GetRulesManager()->GetTelNumModel());
62     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Deafult Profile: " << default_profile;
63 }
64
65 /** ChangeTeNulProfile
66 Slot for signal from class for tel num profil to set
67 \param profile_name name of profile
68 */
69 void ControlClass::ChangeTelNumProfile(QString profile_name)
70 {
71     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Timer set";
72     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "name of timed profile:" << profile_name;
73     telnum_profile = profile_name;
74     //emit s_timed_profile_name(timer_profile); //signal for MainWindow
75     ChooseProfile();
76 }
77
78
79 /** ChangeTimedProfile
80 Slot for signal from class for timed profil to set
81 \param profile_name name of profile
82 */
83 void ControlClass::ChangeTimedProfile(QString profile_name)
84 {
85     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Timer set";
86     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "name of timed profile:" << profile_name;
87     timer_profile = profile_name;
88     emit s_timed_profile_name(timer_profile); //signal for MainWindow
89     ChooseProfile();
90 }
91
92 /** ChangeNetworkProfile
93 Slot for signal from class for network profil to set
94 */
95 void ControlClass::ChangeNetworkProfile(QString profile_name)
96 {
97     network_profile = profile_name;
98     emit s_network_profile_name(network_profile);
99     ChooseProfile();
100 }
101
102 /** ChangeCalendarProfile
103 Slot for signal from class for caledar profil to set
104 \param profile_name name of profile
105 */
106 void ControlClass::ChangeCalendarProfile(QString profile_name)
107 {
108     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "Calendar set";
109     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "name of calendar profile:" << profile_name;
110     calendar_profile = profile_name;
111     emit s_calendar_profile_name(calendar_profile);
112     ChooseProfile();
113 }
114
115
116 /** SetDefaultProfile(QString profile).
117 Method for setting default profile
118 \param profile name of profile
119 */
120 void ControlClass::SetDefaultProfile(QString profile)
121 {
122     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "profil default: " << profile;
123     //not to set same profile
124     if(profile != default_profile){
125
126         settings->beginGroup("ControlClass");
127         settings->setValue("DefaultProfile", profile);
128         settings->endGroup();
129         ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "default profile will be set: " << profile;
130         default_profile = profile;
131         ChooseProfile();
132     }
133 }
134
135 /** SetTelNum
136 Show network dialog
137 */
138 void ControlClass::SetTelNum()
139 {
140     p_telenum_profil->ShowDialog();
141 }
142
143
144 /** SetNetwork
145 Show network dialog
146 */
147 void ControlClass::SetNetwork()
148 {
149     p_network_profile->ShowDialog();
150 }
151
152 /** SetCalendar
153 Show calendat dialog
154 */
155 void ControlClass::SetCalendar()
156 {
157     p_calendar_profile->ShowDialog();
158 }
159
160 /** SetTimeProfile
161 Call time profile class to show dialog to set timed profile
162 */
163 void ControlClass::SetTimeProfile()
164 {
165     p_time_profile->SetTimer(p_profile_db->GetProfileManager()->GetProfilesNames());
166 }
167
168
169 /** ChooseProfile().
170 This method will chosse profile from default_profile, timer_profile and calendar_profile
171 based of priority
172 */
173 void ControlClass::ChooseProfile()
174 {
175     // QString textShow = tr("Current profile is: ");
176     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " timer profile empty  " << timer_profile.isEmpty();
177     if(!telnum_profile.isEmpty()){
178         if(telnum_profile != actual_set_profile){
179             p_profile_db->GetProfileManager()->SetProfile(telnum_profile);
180             actual_set_profile = telnum_profile;
181             ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "time profile waill be set now";
182         }
183     } else if(!timer_profile.isEmpty()){
184         if(timer_profile != actual_set_profile){
185             if(telnum_profile.isEmpty()){
186                 emit s_profile_now(QObject::tr("Timer profile: ") + timer_profile);
187             }
188             p_profile_db->GetProfileManager()->SetProfile(timer_profile);
189             actual_set_profile = timer_profile;
190             ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "time profile waill be set now";
191         }
192     }else if(!network_profile.isEmpty())
193     {
194         if(network_profile != actual_set_profile)
195         {
196             if(telnum_profile.isEmpty()){
197                 emit s_profile_now(QObject::tr("Network profile: ") + network_profile);
198             }
199             ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "network profile will be set";
200             p_profile_db->GetProfileManager()->SetProfile(network_profile);
201             actual_set_profile = network_profile;
202         }
203     }
204     else if(!calendar_profile.isEmpty())
205     {
206         if(calendar_profile != actual_set_profile) {
207             if(telnum_profile.isEmpty()){
208                 emit s_profile_now(QObject::tr("Calendar profile: ") + calendar_profile);
209             }
210             p_profile_db->GetProfileManager()->SetProfile(calendar_profile);
211             actual_set_profile = calendar_profile;
212
213         }
214     } else {
215         if(default_profile != actual_set_profile) {
216             if(telnum_profile.isEmpty()){
217                 emit s_profile_now(QObject::tr("Default profile: ") + default_profile);
218             }
219             ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "deafult profile will be set";
220             p_profile_db->GetProfileManager()->SetProfile(default_profile);
221             actual_set_profile = default_profile;
222
223         }
224     }
225 }
226 //====================================================================
227
228 /**  ChangeInProfile
229 Slot for change in profile
230 \param profile name of profile
231 */
232 void ControlClass::ChangeInProfile(QString profile)
233 {
234     if(actual_set_profile == profile ) p_profile_db->GetProfileManager()->SetProfile(profile);
235 }
236 /** GetProfileDB()
237 Returns the object reference for the database
238 \returns pointer to ProfileDB
239 */
240 ProfileDB * ControlClass::GetProfileDB()
241 {
242     return p_profile_db;
243 }
244
245 /** GetDefaultProfile()
246 Returns the name of default profile
247 \returns name of default profile
248 */
249 QString ControlClass::GetDefaultProfile()
250 {
251     return default_profile;
252 }
253
254 /** GetTimedProfile()
255 Returns the name of timed profile
256 \returns name of timed profile
257 */
258 QString ControlClass::GetTimedProfile()
259 {
260     return timer_profile;
261 }
262
263 /** GetCalendarProfile()
264 Returns the name of calendar profile
265 \returns name of calendar profile
266 */
267 QString ControlClass::GetCalendarProfile()
268 {
269     return calendar_profile;
270 }