N9profile
[n9profile] / telephonenumnewruledialog.cpp
1 #include "telephonenumnewruledialog.h"
2 #include "ui_telephonenumnewruledialog.h"
3 #include <QStringList>
4 #include <QtCore/QSettings>
5 #include <QtCore/QDebug> //Debug pro informace
6 #include <QtGui/QItemSelectionModel>
7 #include <QtGui/QStandardItemModel>
8 #include <QMaemo5InformationBox>
9 #include <QMaemo5ValueButton>
10 #include <QMaemo5ListPickSelector>
11 #include <QMaemo5InformationBox>
12 TelePhoneNumNewRuleDialog::TelePhoneNumNewRuleDialog(QWidget *parent) :
13         QDialog(parent),
14         ui(new Ui::TelePhoneNumNewRuleDialog)
15 {
16     ui->setupUi(this);
17     selectedProfile = QString("");
18     ui->lineEditTelNumber->setText(QString("+420"));
19 }
20
21 TelePhoneNumNewRuleDialog::~TelePhoneNumNewRuleDialog()
22 {
23     delete ui;
24 }
25
26
27 /** SetProfilenames.
28   Store profile names and create maemo button fro show this names
29   \param list_of_profiles_nf list of profile names
30 */
31 void  TelePhoneNumNewRuleDialog::SetProfilenames(QStringList list_of_profiles_nf)
32 {
33     list_of_profiles = list_of_profiles_nf;
34
35     p_model_profile_names = new QStandardItemModel(this);//model for maemo 5 button
36
37     foreach(QString profile, list_of_profiles )
38     {
39         p_model_profile_names->appendRow(new QStandardItem(profile));
40     }
41
42     p_set_Profile_button = new QMaemo5ValueButton(tr("Choose profile for Number:"), this);
43     p_set_Profile_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
44     p_list_pick_profile = new QMaemo5ListPickSelector();
45     p_list_pick_profile->setModel(p_model_profile_names);
46     p_set_Profile_button->setPickSelector(p_list_pick_profile);
47     ui->formLayout_2->addWidget(p_set_Profile_button);
48
49     connect(p_list_pick_profile,SIGNAL(selected(QString)),this,SLOT(SelectedName(QString)));
50 }
51
52 /** SetModel.
53 set model for dialog
54 \param model  model
55 */
56 void TelePhoneNumNewRuleDialog::SetModel(QStandardItemModel *model)
57 {
58     model_of_Telnum = model;
59 }
60
61
62 /** TelePhoneNumNewRuleDialog.
63 number in table
64 \param Tel  tel number
65 */
66 bool TelePhoneNumNewRuleDialog::FindTelModel(QString Tel)
67 {
68     QList<QStandardItem *> list_of_num;
69
70     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "Bude hledat modelu wifi site " ;
71     //qDebug()<< "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "pocet radku  v modelu wifi " << model_of_WifiIf->rowCount();
72     //labels << tr("Name") << tr("Wifi name") << tr("Profile");
73     list_of_num = model_of_Telnum->findItems(Tel, Qt::MatchExactly , 1); //fing num name in 1 colum
74     return list_of_num.empty();
75
76 }
77
78
79 /** SelectedName.
80   Slot for change in list pick
81   \param name name of profile
82 */
83 void TelePhoneNumNewRuleDialog::SelectedName(QString name)
84 {
85     selectedProfile = name;
86 }
87
88 /** SetNameOfLabel.
89 Set text for label
90   \param id name of network
91   \param name text for label
92 */
93 void  TelePhoneNumNewRuleDialog::SetNameOfLabel(QString id, QString name )
94 {
95     ui->labelText->setText(name + id);
96 }
97
98 /** GetNameProfile.
99 Return name of selected profile
100 */
101 QString TelePhoneNumNewRuleDialog::GetNameProfile()
102 {
103     return selectedProfile;
104 }
105
106 /** GetNameOfRule.
107 Return nameo of rule
108 */
109 QString TelePhoneNumNewRuleDialog::GetNameOfRule()
110 {
111     return ui->lineEditName->text();
112 }
113
114 /** GetNameOfRule.
115 Return nameo of rule
116 */
117 QString TelePhoneNumNewRuleDialog::GetTelNum()
118 {
119     return ui->lineEditTelNumber->text();
120 }
121
122 /** GetNameOfRule.
123 Return nameo of rule
124 */
125 void TelePhoneNumNewRuleDialog::SetTelNum(QString str)
126 {
127     ui->lineEditTelNumber->setText(str);
128 }
129
130 /** SetNameOfLabel.
131 set namo of rule
132   \param name text for label
133 */
134 void TelePhoneNumNewRuleDialog::SetNameOfRule(QString name)
135 {
136     ui->lineEditName->setText(name);
137 }
138
139 /** accept()
140 Slot for signal when user click on save button.
141 Check is everyting is OK
142 */
143 void TelePhoneNumNewRuleDialog::accept()
144 {
145     if(selectedProfile.isEmpty())
146     {
147         QMaemo5InformationBox::information(this, tr("Select profile for number"), QMaemo5InformationBox::DefaultTimeout);
148         return;
149     }
150
151     if(ui->lineEditName->text().isEmpty())
152     {
153         QMaemo5InformationBox::information(this, tr("Please fill name"), QMaemo5InformationBox::DefaultTimeout);
154         return;
155     }
156
157     if(ui->lineEditTelNumber->text().split(QRegExp("\\s+"),QString::SkipEmptyParts).size() > 1)
158     {
159         QMaemo5InformationBox::information(this, tr("One number please"), QMaemo5InformationBox::DefaultTimeout);
160         return;
161     }
162
163     if((ui->lineEditTelNumber->text().length() == 13))
164     {
165
166         QString str = ui->lineEditTelNumber->text();
167         if(str.contains(QRegExp("[+]\\d{12}")))
168         {
169             if(FindTelModel(str)){
170                 done(QDialog::Accepted);
171             }else {
172                 QMaemo5InformationBox::information(this, tr("This rule for number already exists"), QMaemo5InformationBox::DefaultTimeout);
173                 return;
174             }
175         } else {
176             QMaemo5InformationBox::information(this, tr("Set number in format : +XXXXXXXXXXXX"), QMaemo5InformationBox::DefaultTimeout);
177             return;
178         }
179     }else {
180         QMaemo5InformationBox::information(this, tr("Set number in format : +XXXXXXXXXXXX"), QMaemo5InformationBox::DefaultTimeout);
181         return;
182     }
183 }
184
185 void TelePhoneNumNewRuleDialog::changeEvent(QEvent *e)
186 {
187     QDialog::changeEvent(e);
188     switch (e->type()) {
189     case QEvent::LanguageChange:
190         ui->retranslateUi(this);
191         break;
192     default:
193         break;
194     }
195 }