first release
[groupsms] / sms / mainwindow.cpp
diff --git a/sms/mainwindow.cpp b/sms/mainwindow.cpp
new file mode 100644 (file)
index 0000000..4471e01
--- /dev/null
@@ -0,0 +1,303 @@
+#include "mainwindow.h"
+#include "newgroupdialog.h"
+#include "addcontacttogroup.h"
+#include "contactinterface.h"
+#include "common.h"
+#include "ui_mainwindow.h"
+#include "utility.h"
+
+#ifdef ONLY_FOR_TELEPATHYQT4
+#include "tpsession/tpsession.h"
+#endif
+
+MainWindow* MainWindow::instance = 0;
+
+MainWindow* MainWindow::getInstance()
+{
+    if (!instance) {
+        instance = new MainWindow();
+    }
+    return instance;
+}
+
+MainWindow::MainWindow(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::MainWindow)
+{
+    instance = this;
+
+    ui->setupUi(this);
+    init();
+}
+
+MainWindow::~MainWindow()
+{
+    delete ui;
+}
+
+void MainWindow::changeEvent(QEvent *e)
+{
+    //qDebug() << "MainWindow::changeEvent(QEvent *e)";
+
+    QMainWindow::changeEvent(e);
+    switch (e->type()) {
+    case QEvent::LanguageChange:
+        ui->retranslateUi(this);
+        break;
+    default:
+        break;
+    }
+}
+
+void MainWindow::init()
+{
+    //qDebug() << "MainWindow::init()";
+
+    ui->btn_send_message->setDisabled(true);
+    ui->textEdit_phone_numbers->setReadOnly(true);
+    connect( ui->textEdit_message, SIGNAL( textChanged() ), this, SLOT( send_message_enabled() ) );
+    connect( ui->textEdit_phone_numbers, SIGNAL( textChanged() ), this, SLOT( send_message_enabled() ) );
+    connect( ui->btn_send_to, SIGNAL( clicked() ), this, SLOT( send_to_clicked() ) );
+    connect( ui->btn_send_message, SIGNAL( clicked() ), this, SLOT( send_message_clicked() ) );
+    connect( ui->btn_cancel, SIGNAL( clicked() ), this, SLOT( cancel_clicked() ) );
+
+    hbox_layout = new QHBoxLayout(this);
+
+    btn_new_group = new QToolButton(this);
+    btn_new_group->setToolTip( tr("new group") );
+    btn_new_group->setText("");
+    btn_new_group->setIcon(Utility::getToolButtonIcon(":/images/newgroup.png", true));
+    btn_new_group->setToolButtonStyle(Qt::ToolButtonIconOnly);
+    btn_new_group->setAutoRaise(true);
+    btn_new_group->show();
+    connect( btn_new_group, SIGNAL( clicked() ), this, SLOT( new_group() ) );
+
+
+    btn_add_contact_to_group = new QToolButton(this);
+    btn_add_contact_to_group->setToolTip( tr("add contact to group") );
+    btn_add_contact_to_group->setText("");
+    btn_add_contact_to_group->setIcon(Utility::getToolButtonIcon(":/images/addcontacttogroup.png", true));
+    btn_add_contact_to_group->setToolButtonStyle(Qt::ToolButtonIconOnly);
+    btn_add_contact_to_group->setAutoRaise(true);
+    btn_add_contact_to_group->show();
+    connect( btn_add_contact_to_group, SIGNAL( clicked() ), this, SLOT( add_contact_to_group() ) );
+
+    btn_delete_contact = new QToolButton(this);
+    btn_delete_contact->setToolTip( tr("delete contacts") );
+    btn_delete_contact->setText("");
+    btn_delete_contact->setIcon(Utility::getToolButtonIcon(":/images/recycle.png"));
+    btn_delete_contact->setToolButtonStyle(Qt::ToolButtonIconOnly);
+    btn_delete_contact->setAutoRaise(true);
+    btn_delete_contact->setDisabled(true);
+    btn_delete_contact->show();
+    connect( btn_delete_contact, SIGNAL(clicked()), this, SLOT(removeSelectedContact()) );
+
+//    btn_delete_group = new QToolButton(this);
+//    btn_delete_group->setToolTip( tr("delete group") );
+//    btn_delete_group->setText("");
+//    btn_delete_group->setIcon(Utility::getToolButtonIcon(":/images/recycle.png"));
+//    btn_delete_group->setToolButtonStyle(Qt::ToolButtonIconOnly);
+//    btn_delete_group->setAutoRaise(true);
+//    btn_delete_group->show();
+
+    btn_sync_contacts = new QToolButton(this);
+    btn_sync_contacts->setToolTip( tr("sync contacts") );
+    btn_sync_contacts->setText("");
+    btn_sync_contacts->setIcon(Utility::getToolButtonIcon(":/images/editgroup.png", true));
+    btn_sync_contacts->setToolButtonStyle(Qt::ToolButtonIconOnly);
+    btn_sync_contacts->setAutoRaise(true);
+    btn_sync_contacts->show();
+    connect( btn_sync_contacts, SIGNAL( clicked() ), this, SLOT( sync_contacts() ) );
+
+    hbox_layout->addWidget( btn_new_group );
+    hbox_layout->addWidget( btn_add_contact_to_group );
+    hbox_layout->addWidget( btn_delete_contact );
+//    hbox_layout->addWidget( btn_delete_group );
+    hbox_layout->addWidget( btn_sync_contacts );
+
+    ui->groupBox->setTitle("");
+    ui->groupBox->setLayout( hbox_layout );
+    ui->groupBox->setEnabled(true);
+
+    contactPage = new ContactPage(this);
+    ui->tabWidget->insertTab(0, contactPage, contactPage->title());
+    ui->tabWidget->setCurrentIndex(0);
+    connect( ui->tabWidget, SIGNAL( currentChanged(int) ), this, SLOT( tab_changed(int) ) );
+    connect( contactPage, SIGNAL( validRecycle(bool) ), this, SLOT( onValidRecyele(bool) ) );
+
+    ui->vLayout_main->setAlignment(Qt::AlignCenter);
+    ui->sendSMS_tab->setLayout(ui->vLayout_main);
+
+    QString filename( HOME_DIR + "/group.xml" );
+    QFileInfo fileinfo(filename);
+    if( !fileinfo.exists() )
+    {
+        XmlControler::getInstance()->newXml( filename );
+#ifdef ONLY_FOR_EBOOK
+        contactPage->setContactItemObserver();
+        ContactInterface::getInstance()->updateContactsFromEbookToXml();
+#endif
+    }else
+    {
+        XmlControler::getInstance()->readXml( filename );
+        // for test xmlcontroler
+//        XmlControler::getInstance()->createGroup( "second_group" );
+//        XmlControler::getInstance()->createContact( "second_group", new ContactWidgetItem);
+//        XmlControler::getInstance()->createGroup( "one_group" );
+//        XmlControler::getInstance()->removeContact( "one_group", NULL);
+//        XmlControler::getInstance()->removeGroup( "one_group" );
+    }
+    contactPage->initContactWidget();
+    contactPage->update();
+
+    //qDebug() << "new SendSMSSession";
+#ifdef ONLY_FOR_TELEPATHYQT4
+    sendSMS = new SendSMSSession( false, this );
+    connect( sendSMS, SIGNAL(  smsSent(QString) ), this, SLOT( onSMSSent(QString) ) );
+#endif
+}
+
+void MainWindow::onSMSSent(QString msg)
+{
+    Q_UNUSED( msg )
+    //qDebug() << "MainWindow::onSMSSent" << msg << "OK";
+}
+
+void MainWindow::setSendToText(QVector<ContactWidgetItem *> *contacts)
+{
+    int count = contacts->size();
+    if( count < 1 )
+        return;
+    QString text;
+    ui->textEdit_phone_numbers->clear();
+    for( int i = 0; i < count; i++ )
+    {
+        text = contacts->at(i)->full_name + ";";
+        name_list.append( text );
+        ui->textEdit_phone_numbers->append( text );
+    }
+}
+
+void MainWindow::sync_contacts()
+{
+#ifdef ONLY_FOR_EBOOK
+    ContactInterface::getInstance()->updateContactsFromEbookToXml();
+#endif
+}
+
+void MainWindow::tab_changed(int index)
+{
+    if( 0 == index ) //contactpage
+    {
+        ui->groupBox->setShown(true);
+    }else //sendsms
+    {
+        ui->groupBox->setHidden(true);
+
+        setSendToText( contactPage->getSelectedContacts() );
+    }
+}
+
+void MainWindow::cancel_clicked()
+{
+    ui->textEdit_phone_numbers->document()->clear();
+    ui->textEdit_message->document()->clear();
+    ui->btn_send_message->setDisabled(true);
+    contactPage->cleanSelectedContactList();
+}
+
+void MainWindow::send_to_clicked()
+{
+    //qDebug() << "MainWindow::send_to_clicked()";
+}
+
+void MainWindow::send_message_clicked()
+{
+    //qDebug() << "MainWindow::send_message_clicked()";
+
+    QTextDocument *doc_phone_number = ui->textEdit_phone_numbers->document();
+    QTextDocument *doc_message = ui->textEdit_message->document();
+    if( !doc_message->isEmpty() && !doc_phone_number->isEmpty() )
+    {
+#ifdef ONLY_FOR_TELEPATHYQT4
+        QString txt = doc_message->toPlainText();
+        QVector<ContactWidgetItem *> *contacts = contactPage->getSelectedContacts();
+        QStringList addrs;
+        QStringList msgs;
+        for( int i = 0; i < contacts->size(); i++ )
+        {
+            //qDebug() << " send sms:" << txt << "to" << contacts->at(i)->mobile_number;
+            addrs.append( contacts->at(i)->mobile_number );
+            msgs.append( txt );
+
+        }
+        sendSMS->setSMSToSend( addrs, msgs );
+#endif
+        // for test
+//        TpSession *tps =new TpSession("ring",true);
+//        tps->sendMessageToAddress("ring",contacts->at(0)->mobile_number,txt);
+    }    
+    cancel_clicked();
+}
+
+void MainWindow::send_message_enabled()
+{
+    //qDebug() << "MainWindow::send_message_enabled()";
+
+    QTextDocument *doc_phone_number = ui->textEdit_phone_numbers->document();
+    QTextDocument *doc_message = ui->textEdit_message->document();
+    if( !doc_message->isEmpty() && !doc_phone_number->isEmpty() )
+    {
+        ui->btn_send_message->setEnabled(true);
+    }else
+    {
+        ui->btn_send_message->setDisabled(true);
+    }
+}
+
+void MainWindow::new_group()
+{
+    //qDebug() << "MainWindow::new_group()";
+
+    NewGroupDialog dlg;
+    int result = dlg.exec();
+    if( result == QDialog::Accepted )
+    {
+        //qDebug() << "new group is ok";
+    }
+}
+
+void MainWindow::add_contact_to_group()
+{
+    //qDebug() << "MainWindow::add_contact_to_group()";
+    AddContactToGroup dlg;
+    int result = dlg.exec();
+    if( result == QDialog::Accepted )
+    {
+        //qDebug() << "add contact is ok";
+    }
+}
+
+void MainWindow::onValidRecyele(bool valid)
+{
+    if( valid )
+    {
+        btn_delete_contact->setEnabled(true);
+    }else
+    {
+        btn_delete_contact->setDisabled(true);
+    }
+}
+
+void MainWindow::removeSelectedContact()
+{
+    //TODO : add confirm dialog
+    QMessageBox msgBox;
+    msgBox.setText("Do you want to delete that's selected contacts?");
+    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
+    int ret = msgBox.exec();
+    if( ret == QDialog::Accepted )
+    {
+        contactPage->removeSelectedContact();
+    }
+}