Added KOfficemeego dependency
[froff-onlinedoc] / 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 #include "encryptsupport.h"
30
31 #include <QtGui>
32 #include <QX11Info>
33 #include <QSettings>
34
35 #include <QMaemo5InformationBox>
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38
39 LoginWindow::LoginWindow(QWidget *parent)
40     : QDialog(parent),
41       m_authDialog(new Ui_Dialog),
42       settings(new QSettings("freoffice", "online-services-plugin", this)),
43       cipher(new encryptSupport(this))
44 {
45     m_authDialog->setupUi(this);
46
47     QStringList onlineServices;
48     onlineServices << "Google Documents";
49     // Add services here
50     onlineServices << "Slide Share";
51     m_authDialog->comboBox->addItems(onlineServices);
52
53     connect(m_authDialog->loginButton, SIGNAL(clicked()), this, SLOT(loginService()));
54     connect(m_authDialog->comboBox, SIGNAL(activated(int)), this, SLOT(serviceSelected(int)));
55     fillDetails();
56     m_authDialog->userEdit->setInputMethodHints(Qt::ImhNoAutoUppercase);
57     m_authDialog->userEdit->setFocus();
58     show();
59 }
60
61 LoginWindow::~LoginWindow()
62 {
63     settings->sync();
64 }
65
66 void LoginWindow::setOpenDoc(const QString & openDocPath)
67 {
68     this->openDocPath = openDocPath;
69 }
70
71 void LoginWindow::loginService()
72 {
73     if("" == m_authDialog->userEdit->text() || "" == m_authDialog->passwordEdit->text()) {
74         QMaemo5InformationBox::information(this, "Enter both username and password", QMaemo5InformationBox::DefaultTimeout);
75         return;
76     }
77     disableWidgets();
78     if(0 == m_authDialog->comboBox->currentIndex()) {
79         gdoc = new GoogleDocumentService();
80         setShowProgressIndicator(true);
81         gdoc->clientLogin(m_authDialog->userEdit->text(), m_authDialog->passwordEdit->text());
82         connect(gdoc, SIGNAL(userAuthenticated(bool)), this, SLOT(authenticated(bool)));
83     }
84     if(1 == m_authDialog->comboBox->currentIndex()) {
85         service = new SlideShare(this);
86         service->setApikey(new QString("KhSMYyiF"));
87         service->setsecretKey(new QString("HDXhAC5g"));
88         service->setUsername(new QString(m_authDialog->userEdit->text()));
89         service->setPassword(new QString(m_authDialog->passwordEdit->text()));
90         service->login();
91         setShowProgressIndicator(true);
92         connect(service, SIGNAL(loginDone(bool)), this, SLOT(slideShareLoginDoneSlot(bool)));
93     }
94 }
95
96 void LoginWindow::serviceSelected(int index)
97 {
98     if(index == 0) {
99         m_authDialog->userEdit->setText("@gmail.com");
100         m_authDialog->passwordEdit->clear();
101     } else if(index == 1) {
102         m_authDialog->userEdit->clear();
103         m_authDialog->passwordEdit->clear();
104     }
105     fillDetails();
106     m_authDialog->userEdit->setFocus();
107 }
108
109 void LoginWindow::authenticated(bool success)
110 {
111     if(success) {
112         QString key("user/gdocs");
113         saveDetails(key);
114         googleListDialog *ld = new googleListDialog(gdoc, this);
115         ld->setOpenDoc(openDocPath);
116         this->accept();;
117         ld->show();
118     } else {
119         QMaemo5InformationBox::information(this, ("<p>Login Failed</p><p>Check your username & password</p>"), QMaemo5InformationBox::NoTimeout);
120         enableWidgets();
121     }
122 }
123
124 void LoginWindow::setShowProgressIndicator(bool visible)
125 {
126     unsigned long val = visible ? 1 : 0;
127     Atom atom = XInternAtom(QX11Info::display(),
128                             "_HILDON_WM_WINDOW_PROGRESS_INDICATOR", False);
129     XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
130                     32, PropModeReplace,
131                     (unsigned char *) &val, 1);
132 }
133
134 void LoginWindow::slideShareLoginDoneSlot(bool loginStatus)
135 {
136     if(loginStatus == false) {
137         QMaemo5InformationBox::information(this, "<p>Login Failed.</p><p>Check your username and password</p> ", QMaemo5InformationBox::NoTimeout);
138         enableWidgets();
139     } else {
140         QString key("user/slideshare");
141         saveDetails(key);
142         slideshareListDialog * ld = new slideshareListDialog(service, this);
143         ld->setOpenDoc(openDocPath);
144         this->accept();
145         ld->show();
146     }
147 }
148
149 void LoginWindow::enableWidgets()
150 {
151     setShowProgressIndicator(false);
152     m_authDialog->loginButton->setEnabled(true);
153     m_authDialog->userEdit->setEnabled(true);
154     m_authDialog->passwordEdit->setEnabled(true);
155     m_authDialog->comboBox->setEnabled(true);
156     m_authDialog->saveCheckBox->setEnabled(true);
157 }
158
159 void LoginWindow::disableWidgets()
160 {
161     m_authDialog->loginButton->setEnabled(false);
162     m_authDialog->userEdit->setEnabled(false);
163     m_authDialog->passwordEdit->setEnabled(false);
164     m_authDialog->comboBox->setEnabled(false);
165     m_authDialog->saveCheckBox->setEnabled(false);
166 }
167
168 void LoginWindow::saveDetails(QString &key)
169 {
170     if(Qt::Checked == m_authDialog->saveCheckBox->checkState()) {
171         QVariantMap m;
172         QString username = m_authDialog->userEdit->text();
173         QString password = m_authDialog->passwordEdit->text();
174         m.insert("username", username);
175         m.insert("password", cipher->encrypt(password));
176         settings->setValue(key, m);
177     } else {
178         settings->remove(key);
179     }
180 }
181
182 void LoginWindow::fillDetails()
183 {
184     QString key = "";
185     if(0 == m_authDialog->comboBox->currentIndex())
186         key = "user/gdocs";
187     else if(1 == m_authDialog->comboBox->currentIndex())
188         key = "user/slideshare";
189     else
190         return;
191     if(settings->contains(key)) {
192         QVariantMap m = settings->value(key).value<QVariantMap>();
193         QString username = m.value("username").toString();
194         QString password = m.value("password").toString();
195         m_authDialog->userEdit->setText(username);
196         m_authDialog->passwordEdit->setText(cipher->decrypt(password));
197         m_authDialog->saveCheckBox->setCheckState(Qt::Checked);
198     } else
199         m_authDialog->saveCheckBox->setCheckState(Qt::Unchecked);
200     return;
201 }