Initial Commit.
[onlineservices] / loginwindow.cpp
1 /*
2  * This file is part of Maemo 5 Office UI for KOffice
3  *
4  * Copyright (C) 2010 Manikandaprasad N C <maninc@gmail.com>.
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include "loginwindow.h"
25 #include "googledocumentservice.h"
26 #include "googlelistdialog.h"
27 #include "slideshare.h"
28 #include "slidesharelistdialog.h"
29
30 #include <QtGui>
31 #include <QX11Info>
32
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35
36 LoginWindow::LoginWindow(QWidget *parent)
37         : QDialog(parent),
38           m_authDialog(new Ui_Dialog)
39 {
40     m_authDialog->setupUi(this);
41
42     QStringList onlineServices;
43     onlineServices << "Google Documents";
44     // Add services here
45     onlineServices << "Slide Share";
46     m_authDialog->comboBox->addItems(onlineServices);
47
48     connect(m_authDialog->loginButton, SIGNAL(clicked()), this, SLOT(loginService()));
49     connect(m_authDialog->comboBox, SIGNAL(activated(int)), this, SLOT(serviceSelected(int)));
50
51     m_authDialog->userEdit->setFocus();
52     show();
53 }
54
55 void LoginWindow::loginService()
56 {
57     disableWidgets();
58     if (0 == m_authDialog->comboBox->currentIndex()) {
59         gdoc = new GoogleDocumentService();
60         setShowProgressIndicator(true);
61         gdoc->clientLogin(m_authDialog->userEdit->text(), m_authDialog->passwordEdit->text());
62         connect(gdoc, SIGNAL(userAuthenticated(bool)), this, SLOT(authenticated(bool)));
63     }
64     if (1 == m_authDialog->comboBox->currentIndex())
65     {
66         service = new SlideShare(this);
67         service->setApikey(new QString("KhSMYyiF"));
68         service->setsecretKey(new QString("HDXhAC5g"));
69         service->setUsername(new QString(m_authDialog->userEdit->text()));
70         service->setPassword(new QString(m_authDialog->passwordEdit->text()));
71         service->login();
72         connect(service,SIGNAL(loginDone(bool)), this, SLOT(slideShareLoginDoneSlot(bool)));
73     }
74 }
75
76 void LoginWindow::serviceSelected(int index)
77 {
78     if (index == 0) {
79         m_authDialog->userEdit->setText("@gmail.com");
80         m_authDialog->passwordEdit->clear();
81     } else if (index == 1) {
82         m_authDialog->userEdit->clear();
83         m_authDialog->passwordEdit->clear();
84     }
85     m_authDialog->userEdit->setFocus();
86 }
87
88 void LoginWindow::authenticated(bool success)
89 {
90     if (success) {
91         googleListDialog *ld = new googleListDialog(gdoc, this);
92         this->accept();;
93         ld->show();
94     }
95     else {
96         QMessageBox::information(this, tr("Login Failed"), tr("Check your username & password"));
97         enableWidgets();
98     }
99 }
100
101 void LoginWindow::setShowProgressIndicator(bool visible)
102 {
103     unsigned long val = visible ? 1 : 0;
104     Atom atom = XInternAtom(QX11Info::display(),
105                             "_HILDON_WM_WINDOW_PROGRESS_INDICATOR", False);
106     XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
107                     32, PropModeReplace,
108                     (unsigned char *) &val, 1);
109 }
110
111 void LoginWindow::slideShareLoginDoneSlot(bool loginStatus)
112 {if(loginStatus == false)
113     {
114         QMessageBox::information(this, tr("Login Failed"), tr("Check your username & password"));
115         enableWidgets();
116     }
117     else
118     {
119         slideshareListDialog * fi = new slideshareListDialog(service, this);
120         this->accept();
121         fi->show();
122     }
123 }
124
125 void LoginWindow::enableWidgets()
126 {
127     m_authDialog->loginButton->setEnabled(true);
128     m_authDialog->userEdit->setEnabled(true);
129     m_authDialog->passwordEdit->setEnabled(true);
130     m_authDialog->comboBox->setEnabled(true);
131 }
132
133 void LoginWindow::disableWidgets()
134 {
135     m_authDialog->loginButton->setEnabled(false);
136     m_authDialog->userEdit->setEnabled(false);
137     m_authDialog->passwordEdit->setEnabled(false);
138     m_authDialog->comboBox->setEnabled(false);
139 }