first release
[groupsms] / sms / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "newgroupdialog.h"
3 #include "addcontacttogroup.h"
4 #include "contactinterface.h"
5 #include "common.h"
6 #include "ui_mainwindow.h"
7 #include "utility.h"
8
9 #ifdef ONLY_FOR_TELEPATHYQT4
10 #include "tpsession/tpsession.h"
11 #endif
12
13 MainWindow* MainWindow::instance = 0;
14
15 MainWindow* MainWindow::getInstance()
16 {
17     if (!instance) {
18         instance = new MainWindow();
19     }
20     return instance;
21 }
22
23 MainWindow::MainWindow(QWidget *parent) :
24     QMainWindow(parent),
25     ui(new Ui::MainWindow)
26 {
27     instance = this;
28
29     ui->setupUi(this);
30     init();
31 }
32
33 MainWindow::~MainWindow()
34 {
35     delete ui;
36 }
37
38 void MainWindow::changeEvent(QEvent *e)
39 {
40     //qDebug() << "MainWindow::changeEvent(QEvent *e)";
41
42     QMainWindow::changeEvent(e);
43     switch (e->type()) {
44     case QEvent::LanguageChange:
45         ui->retranslateUi(this);
46         break;
47     default:
48         break;
49     }
50 }
51
52 void MainWindow::init()
53 {
54     //qDebug() << "MainWindow::init()";
55
56     ui->btn_send_message->setDisabled(true);
57     ui->textEdit_phone_numbers->setReadOnly(true);
58     connect( ui->textEdit_message, SIGNAL( textChanged() ), this, SLOT( send_message_enabled() ) );
59     connect( ui->textEdit_phone_numbers, SIGNAL( textChanged() ), this, SLOT( send_message_enabled() ) );
60     connect( ui->btn_send_to, SIGNAL( clicked() ), this, SLOT( send_to_clicked() ) );
61     connect( ui->btn_send_message, SIGNAL( clicked() ), this, SLOT( send_message_clicked() ) );
62     connect( ui->btn_cancel, SIGNAL( clicked() ), this, SLOT( cancel_clicked() ) );
63
64     hbox_layout = new QHBoxLayout(this);
65
66     btn_new_group = new QToolButton(this);
67     btn_new_group->setToolTip( tr("new group") );
68     btn_new_group->setText("");
69     btn_new_group->setIcon(Utility::getToolButtonIcon(":/images/newgroup.png", true));
70     btn_new_group->setToolButtonStyle(Qt::ToolButtonIconOnly);
71     btn_new_group->setAutoRaise(true);
72     btn_new_group->show();
73     connect( btn_new_group, SIGNAL( clicked() ), this, SLOT( new_group() ) );
74
75
76     btn_add_contact_to_group = new QToolButton(this);
77     btn_add_contact_to_group->setToolTip( tr("add contact to group") );
78     btn_add_contact_to_group->setText("");
79     btn_add_contact_to_group->setIcon(Utility::getToolButtonIcon(":/images/addcontacttogroup.png", true));
80     btn_add_contact_to_group->setToolButtonStyle(Qt::ToolButtonIconOnly);
81     btn_add_contact_to_group->setAutoRaise(true);
82     btn_add_contact_to_group->show();
83     connect( btn_add_contact_to_group, SIGNAL( clicked() ), this, SLOT( add_contact_to_group() ) );
84
85     btn_delete_contact = new QToolButton(this);
86     btn_delete_contact->setToolTip( tr("delete contacts") );
87     btn_delete_contact->setText("");
88     btn_delete_contact->setIcon(Utility::getToolButtonIcon(":/images/recycle.png"));
89     btn_delete_contact->setToolButtonStyle(Qt::ToolButtonIconOnly);
90     btn_delete_contact->setAutoRaise(true);
91     btn_delete_contact->setDisabled(true);
92     btn_delete_contact->show();
93     connect( btn_delete_contact, SIGNAL(clicked()), this, SLOT(removeSelectedContact()) );
94
95 //    btn_delete_group = new QToolButton(this);
96 //    btn_delete_group->setToolTip( tr("delete group") );
97 //    btn_delete_group->setText("");
98 //    btn_delete_group->setIcon(Utility::getToolButtonIcon(":/images/recycle.png"));
99 //    btn_delete_group->setToolButtonStyle(Qt::ToolButtonIconOnly);
100 //    btn_delete_group->setAutoRaise(true);
101 //    btn_delete_group->show();
102
103     btn_sync_contacts = new QToolButton(this);
104     btn_sync_contacts->setToolTip( tr("sync contacts") );
105     btn_sync_contacts->setText("");
106     btn_sync_contacts->setIcon(Utility::getToolButtonIcon(":/images/editgroup.png", true));
107     btn_sync_contacts->setToolButtonStyle(Qt::ToolButtonIconOnly);
108     btn_sync_contacts->setAutoRaise(true);
109     btn_sync_contacts->show();
110     connect( btn_sync_contacts, SIGNAL( clicked() ), this, SLOT( sync_contacts() ) );
111
112     hbox_layout->addWidget( btn_new_group );
113     hbox_layout->addWidget( btn_add_contact_to_group );
114     hbox_layout->addWidget( btn_delete_contact );
115 //    hbox_layout->addWidget( btn_delete_group );
116     hbox_layout->addWidget( btn_sync_contacts );
117
118     ui->groupBox->setTitle("");
119     ui->groupBox->setLayout( hbox_layout );
120     ui->groupBox->setEnabled(true);
121
122     contactPage = new ContactPage(this);
123     ui->tabWidget->insertTab(0, contactPage, contactPage->title());
124     ui->tabWidget->setCurrentIndex(0);
125     connect( ui->tabWidget, SIGNAL( currentChanged(int) ), this, SLOT( tab_changed(int) ) );
126     connect( contactPage, SIGNAL( validRecycle(bool) ), this, SLOT( onValidRecyele(bool) ) );
127
128     ui->vLayout_main->setAlignment(Qt::AlignCenter);
129     ui->sendSMS_tab->setLayout(ui->vLayout_main);
130
131     QString filename( HOME_DIR + "/group.xml" );
132     QFileInfo fileinfo(filename);
133     if( !fileinfo.exists() )
134     {
135         XmlControler::getInstance()->newXml( filename );
136 #ifdef ONLY_FOR_EBOOK
137         contactPage->setContactItemObserver();
138         ContactInterface::getInstance()->updateContactsFromEbookToXml();
139 #endif
140     }else
141     {
142         XmlControler::getInstance()->readXml( filename );
143         // for test xmlcontroler
144 //        XmlControler::getInstance()->createGroup( "second_group" );
145 //        XmlControler::getInstance()->createContact( "second_group", new ContactWidgetItem);
146 //        XmlControler::getInstance()->createGroup( "one_group" );
147 //        XmlControler::getInstance()->removeContact( "one_group", NULL);
148 //        XmlControler::getInstance()->removeGroup( "one_group" );
149     }
150     contactPage->initContactWidget();
151     contactPage->update();
152
153     //qDebug() << "new SendSMSSession";
154 #ifdef ONLY_FOR_TELEPATHYQT4
155     sendSMS = new SendSMSSession( false, this );
156     connect( sendSMS, SIGNAL(  smsSent(QString) ), this, SLOT( onSMSSent(QString) ) );
157 #endif
158 }
159
160 void MainWindow::onSMSSent(QString msg)
161 {
162     Q_UNUSED( msg )
163     //qDebug() << "MainWindow::onSMSSent" << msg << "OK";
164 }
165
166 void MainWindow::setSendToText(QVector<ContactWidgetItem *> *contacts)
167 {
168     int count = contacts->size();
169     if( count < 1 )
170         return;
171     QString text;
172     ui->textEdit_phone_numbers->clear();
173     for( int i = 0; i < count; i++ )
174     {
175         text = contacts->at(i)->full_name + ";";
176         name_list.append( text );
177         ui->textEdit_phone_numbers->append( text );
178     }
179 }
180
181 void MainWindow::sync_contacts()
182 {
183 #ifdef ONLY_FOR_EBOOK
184     ContactInterface::getInstance()->updateContactsFromEbookToXml();
185 #endif
186 }
187
188 void MainWindow::tab_changed(int index)
189 {
190     if( 0 == index ) //contactpage
191     {
192         ui->groupBox->setShown(true);
193     }else //sendsms
194     {
195         ui->groupBox->setHidden(true);
196
197         setSendToText( contactPage->getSelectedContacts() );
198     }
199 }
200
201 void MainWindow::cancel_clicked()
202 {
203     ui->textEdit_phone_numbers->document()->clear();
204     ui->textEdit_message->document()->clear();
205     ui->btn_send_message->setDisabled(true);
206     contactPage->cleanSelectedContactList();
207 }
208
209 void MainWindow::send_to_clicked()
210 {
211     //qDebug() << "MainWindow::send_to_clicked()";
212 }
213
214 void MainWindow::send_message_clicked()
215 {
216     //qDebug() << "MainWindow::send_message_clicked()";
217
218     QTextDocument *doc_phone_number = ui->textEdit_phone_numbers->document();
219     QTextDocument *doc_message = ui->textEdit_message->document();
220     if( !doc_message->isEmpty() && !doc_phone_number->isEmpty() )
221     {
222 #ifdef ONLY_FOR_TELEPATHYQT4
223         QString txt = doc_message->toPlainText();
224         QVector<ContactWidgetItem *> *contacts = contactPage->getSelectedContacts();
225         QStringList addrs;
226         QStringList msgs;
227         for( int i = 0; i < contacts->size(); i++ )
228         {
229             //qDebug() << " send sms:" << txt << "to" << contacts->at(i)->mobile_number;
230             addrs.append( contacts->at(i)->mobile_number );
231             msgs.append( txt );
232
233         }
234         sendSMS->setSMSToSend( addrs, msgs );
235 #endif
236         // for test
237 //        TpSession *tps =new TpSession("ring",true);
238 //        tps->sendMessageToAddress("ring",contacts->at(0)->mobile_number,txt);
239     }    
240     cancel_clicked();
241 }
242
243 void MainWindow::send_message_enabled()
244 {
245     //qDebug() << "MainWindow::send_message_enabled()";
246
247     QTextDocument *doc_phone_number = ui->textEdit_phone_numbers->document();
248     QTextDocument *doc_message = ui->textEdit_message->document();
249     if( !doc_message->isEmpty() && !doc_phone_number->isEmpty() )
250     {
251         ui->btn_send_message->setEnabled(true);
252     }else
253     {
254         ui->btn_send_message->setDisabled(true);
255     }
256 }
257
258 void MainWindow::new_group()
259 {
260     //qDebug() << "MainWindow::new_group()";
261
262     NewGroupDialog dlg;
263     int result = dlg.exec();
264     if( result == QDialog::Accepted )
265     {
266         //qDebug() << "new group is ok";
267     }
268 }
269
270 void MainWindow::add_contact_to_group()
271 {
272     //qDebug() << "MainWindow::add_contact_to_group()";
273     AddContactToGroup dlg;
274     int result = dlg.exec();
275     if( result == QDialog::Accepted )
276     {
277         //qDebug() << "add contact is ok";
278     }
279 }
280
281 void MainWindow::onValidRecyele(bool valid)
282 {
283     if( valid )
284     {
285         btn_delete_contact->setEnabled(true);
286     }else
287     {
288         btn_delete_contact->setDisabled(true);
289     }
290 }
291
292 void MainWindow::removeSelectedContact()
293 {
294     //TODO : add confirm dialog
295     QMessageBox msgBox;
296     msgBox.setText("Do you want to delete that's selected contacts?");
297     msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
298     int ret = msgBox.exec();
299     if( ret == QDialog::Accepted )
300     {
301         contactPage->removeSelectedContact();
302     }
303 }