README
[n9profile] / telephonenumprofile.cpp
1 #include "telephonenumprofile.h"
2 #include "telenumdialog.h"
3 #include <QStringList>
4 #include <QtCore/QSettings>
5 #include <QtCore/QDebug> //Debug pro informace
6 #include <QtGui/QStandardItemModel>
7 #include <QtGui/QItemSelectionModel>
8 #include <QtDBus/QDBusConnection>
9 #include <QDBusMessage>
10 #include <QtCore/QTimer>
11 #include <QDBusInterface>
12 static const char* CALL_DBUS_SERVICE =  "com.nokia.csd.Call";
13 static const char* CALL_DBUS_OBJECT_PATH =  "/com/nokia/csd/call/1";
14 static const char* CALL_DBUS_INTERFACE = "com.nokia.csd.Call.Instance";
15 static const char* CALL_DBUS_METHOD = "GetStatus";
16
17 TelephoneNumProfile::TelephoneNumProfile(QWidget *parent) :
18         QWidget(parent)
19 {
20     //======================================================
21     timer = new QTimer(this);
22     timer->setSingleShot(true);//A single-shot timer fires only once
23     connect(timer,SIGNAL(timeout()),this,SLOT(TimerTimeout()));
24     bool succ =   QDBusConnection::systemBus().connect( "", "/com/nokia/csd/call", "com.nokia.csd.Call", "Coming", this, SLOT(receiveCall(const  QDBusMessage&)));;
25
26     if(succ){
27         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "anooooooooooooooooooooooooooooooooooooooo:";
28     } else {
29         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "neeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee:";
30     }
31 }
32
33 /** receiveCall.
34 Slot for dbus signal for incoming call
35 \param msg msg from d bus
36 */
37 void TelephoneNumProfile::receiveCall(  const  QDBusMessage& msg)
38 {
39     // profile_set_value_as_bool("general", "vibrating.alert.enabled",  false);
40     //profile_set_value("general", "ringing.alert.type",  "silent");
41     // QList<QVariant> lst = msg.arguments();
42     //    QVariant var;
43     //    foreach(var, lst)
44     //    {
45     //        //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" <<  "příchozí hovor"  << var.toString();
46     //    }
47     //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" <<  "příchozí hovor";
48     QString profil_name = FindTelnumInModel(msg.arguments().at(1).toString());
49     if(!profil_name.isEmpty()){
50         emit s_telnum_profile(profil_name);
51         timer->start(1000);
52     }
53 }
54
55 /** TimerTimeout()
56 timer timeout slot
57 */
58 void TelephoneNumProfile::TimerTimeout()
59 {
60    qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "timer  timeout" ;
61
62     QDBusConnection systemDbusConnection = QDBusConnection::systemBus();
63     QDBusInterface connectionInterface(CALL_DBUS_SERVICE,
64                                        CALL_DBUS_OBJECT_PATH,
65                                        CALL_DBUS_INTERFACE,
66                                        systemDbusConnection);
67     if (!connectionInterface.isValid()) {
68         qWarning() << "Cannotct conect to D-BUS";
69         return;
70     }
71     //poslu zpravu ze chcu info
72     QDBusMessage reply = connectionInterface.call(QLatin1String(CALL_DBUS_METHOD));
73     if (reply.type() == QDBusMessage::ReplyMessage) {
74         QList<QVariant> argList = reply.arguments();
75         //   currentCellNetworkStatus = argList.at(INDEX_OF_NEVIM).toInt();
76         foreach(QVariant var, argList)
77         {
78             qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << " hodnota :::" << var.toString();
79         }
80         if(reply.arguments().at(0).toString() != "0")
81         {
82             qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "nebyl nula" ;
83             timer->start(2000);
84             return;
85         }
86         else{
87             qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "text::" << "ted jo" ;
88             emit  s_telnum_profile(QString(""));//send empty string
89         }
90    }
91     else
92     {
93         qWarning() << reply.errorMessage();
94     }
95 }
96
97 /** SetProfilenames.
98 Set profiles names
99 \param list_of_profiles_nf names of profiles
100 */
101 void TelephoneNumProfile::SetProfilenames(QStringList list_of_profiles_nf)
102 {
103     list_of_profiles = list_of_profiles_nf;
104 }
105
106 /** createModels
107 gets model and create selection model
108 */
109 void TelephoneNumProfile::createModels(QStandardItemModel * telenum)
110 {
111     model_of_telenum =  telenum;
112     selModel_telenum = new QItemSelectionModel(model_of_telenum);
113 }
114
115 /** FindIDLocInModel.
116 Find id location and return name of profile
117 \param telnum tele num
118 */
119 QString TelephoneNumProfile::FindTelnumInModel(QString telnum)
120 {
121     QString name = QString("");
122     QList<QStandardItem *> list_of_telnum;
123
124     ////qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "Bude hledat modelu wifi site " ;
125
126     //labels << tr("Name") << tr("Wifi name") << tr("Profile");
127     list_of_telnum = model_of_telenum->findItems(telnum, Qt::MatchExactly , 1);
128
129     if(list_of_telnum.empty())
130     {
131         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "nic nanasel " ;
132         return name;
133     } else
134     {
135         name = model_of_telenum->item(list_of_telnum.at(0)->row(),2)->data(Qt::DisplayRole).toString();
136         //qDebug() << "In file:" <<  __FILE__ << ":" << "on line:" << __LINE__ << " in function:" << __FUNCTION__ << "nasel ho  " << name;
137         return name;
138     }
139 }
140
141
142 //===========================================================================
143 /** ShowDialog
144 For dialog for modifi calendars and profiles
145 */
146 void TelephoneNumProfile::ShowDialog()
147 {
148     TeleNumDialog dialog(this);
149     dialog.SetViews(model_of_telenum,selModel_telenum);
150     dialog.SetProfilenames(list_of_profiles);
151
152     dialog.exec();
153 }
154