Added openssl-dev dependency
[froff-onlinedoc] / googlelistdialog.cpp
1 /*
2  *  Copyright (c) 2010 Kaushal M <kshlmster@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "googlelistdialog.h"
20 #include "ui_filelistdialog.h"
21
22 #include "googledocument.h"
23 #include "googleuploaddialog.h"
24
25 #include <QMaemo5InformationBox>
26 #include <QFileDialog>
27 #include <QDesktopServices>
28
29 googleListDialog::googleListDialog(GoogleDocumentService *service, QWidget *parent):
30     QDialog(parent),
31     ui(new Ui::fileListDialog)
32 {
33     ui->setupUi(this);
34     this->setWindowTitle("Google Docs");
35     this->service = service;
36     ui->listTab->setCurrentIndex(0);
37     ui->downloadProgressBar->setVisible(false);
38
39     connect(ui->downloadButton, SIGNAL(clicked()), this, SLOT(downloadButtonClickedSlot()));
40     connect(ui->uploadButton, SIGNAL(clicked()), this, SLOT(uploadButtonClickedSlot()));
41     connect(ui->refreshButton, SIGNAL(clicked()), this, SLOT(refreshList()));
42     connect(service, SIGNAL(listDone(bool)), this, SLOT(fillList(bool)));
43     connect(service, SIGNAL(downloadDone(bool)), this, SLOT(downloadDoneSlot(bool)));
44     connect(service, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(updateProgressBar(qint64, qint64)));
45     refreshList();
46 }
47
48 void googleListDialog::changeEvent(QEvent *e)
49 {
50     QDialog::changeEvent(e);
51     switch(e->type()) {
52     case QEvent::LanguageChange:
53         ui->retranslateUi(this);
54         break;
55     default:
56         break;
57     }
58 }
59
60 void googleListDialog::refreshList()
61 {
62     ui->listTab->setEnabled(false);
63     ui->downloadButton->setEnabled(false);
64     ui->uploadButton->setEnabled(false);
65     ui->refreshButton->setEnabled(false);
66     ui->downloadProgressBar->setMinimum(0);
67     ui->downloadProgressBar->setMaximum(0);
68     ui->downloadProgressBar->setVisible(true);
69     service->listDocuments();
70 }
71
72 void googleListDialog::fillList(bool status)
73 {
74     if(status) {
75         ui->documentList->clear();
76         ui->presentationList->clear();
77         ui->spreadsheetList->clear();
78         ui->othersList->clear();
79
80         QList<GoogleDocument *> list;
81
82         list = service->documentList;
83         foreach(GoogleDocument * i, list) {
84             ui->documentList->addItem(i->title());
85         }
86
87         list = service->presentationList;
88         foreach(GoogleDocument * i, list) {
89             ui->presentationList->addItem(i->title());
90         }
91
92         list = service->spreadsheetList;
93         foreach(GoogleDocument * i, list) {
94             ui->spreadsheetList->addItem(i->title());
95         }
96
97         list = service->othersList;
98         foreach(GoogleDocument * i, list) {
99             ui->othersList->addItem(i->title());
100         }
101     } else {
102         QMaemo5InformationBox::information(this, "<p><b>Error</b></p> <p>An error occured while fetching document list</p>", QMaemo5InformationBox::NoTimeout);
103     }
104     ui->downloadProgressBar->setMaximum(100);
105     ui->downloadProgressBar->setVisible(false);
106     ui->listTab->setEnabled(true);
107     ui->downloadButton->setEnabled(true);
108     ui->uploadButton->setEnabled(true);
109     ui->refreshButton->setEnabled(true);
110 }
111
112 void googleListDialog::downloadButtonClickedSlot()
113 {
114     QList<GoogleDocument *> list;
115     QListWidget *tmp = 0;
116     switch(ui->listTab->currentIndex()) {
117     case 0:
118         tmp = ui->documentList;
119         list = service->documentList;
120         break;
121     case 1:
122         tmp = ui->presentationList;
123         list = service->presentationList;
124         break;
125     case 2:
126         tmp = ui->spreadsheetList;
127         list = service->spreadsheetList;
128         break;
129     case 3:
130         tmp = ui->othersList;
131         list = service->othersList;
132     }
133
134     if(-1 == tmp->currentRow()) {
135         QMaemo5InformationBox::information(this, "Select a file from the list to download", QMaemo5InformationBox::DefaultTimeout);
136         return;
137     }
138     GoogleDocument *doc = list[tmp->currentRow()];
139     QString format = "";
140     if("presentation" == doc->documentType()) {
141         format = ".ppt";
142     } else if("document" == doc->documentType()) {
143         format = ".odt";
144     } else if("spreadsheet" == doc->documentType()) {
145         format = ".ods";
146     }
147     QString filename = QFileDialog::getSaveFileName(this, "Save File", QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation) + "/" + doc->title() + format, "(*" + format + ")");
148     if("" == filename)
149         return;
150     service->downloadDocument(doc, new QString(filename));
151     ui->listTab->setEnabled(false);
152     ui->uploadButton->setEnabled(false);
153     ui->downloadButton->setEnabled(false);
154     ui->refreshButton->setEnabled(false);
155     ui->downloadProgressBar->setVisible(true);
156 }
157
158 void googleListDialog::uploadButtonClickedSlot()
159 {
160     googleUploadDialog *ud = new googleUploadDialog(service, this);
161     if("" != openDocPath) {
162         ud->setOpenDoc(openDocPath);
163     }
164     ud->show();
165     connect(ud, SIGNAL(accepted()), this, SLOT(refreshList()));
166 }
167
168 void googleListDialog::downloadDoneSlot(bool status)
169 {
170     ui->downloadProgressBar->setVisible(false);
171     ui->downloadProgressBar->setValue(0);
172     ui->listTab->setEnabled(true);
173     ui->downloadButton->setEnabled(true);
174     ui->refreshButton->setEnabled(true);
175     ui->uploadButton->setEnabled(true);
176     if(status)
177         QMaemo5InformationBox::information(this, "The file has finished downloading", QMaemo5InformationBox::DefaultTimeout);
178     else
179         QMaemo5InformationBox::information(this, "<p><b>Error</b></p> <p>An error occured while downloading the document</p>", QMaemo5InformationBox::NoTimeout);
180 }
181
182 void googleListDialog::updateProgressBar(qint64 bytesDone, qint64 bytesTotal)
183 {
184     int value = (bytesDone * 100) / bytesTotal;
185     ui->downloadProgressBar->setValue(value);
186 }
187
188 void googleListDialog::setOpenDoc(const QString & openDocPath)
189 {
190     this->openDocPath = openDocPath;
191 }