Initial Commit.
[onlineservices] / 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 <QMessageBox>
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     }
102     else {
103         QMessageBox::information(this, tr("Error"), tr("An error occured while fetching document list"));
104     }
105     ui->downloadProgressBar->setMaximum(100);
106     ui->downloadProgressBar->setVisible(false);
107     ui->listTab->setEnabled(true);
108     ui->downloadButton->setEnabled(true);
109     ui->uploadButton->setEnabled(true);
110     ui->refreshButton->setEnabled(true);
111 }
112
113 void googleListDialog::downloadButtonClickedSlot()
114 {
115     QList<GoogleDocument *> list;
116     QListWidget *tmp = 0;
117     switch(ui->listTab->currentIndex()) {
118     case 0:
119         tmp = ui->documentList;
120         list = service->documentList;
121         break;
122     case 1:
123         tmp = ui->presentationList;
124         list = service->presentationList;
125         break;
126     case 2:
127         tmp = ui->spreadsheetList;
128         list = service->spreadsheetList;
129         break;
130     case 3:
131         tmp = ui->othersList;
132         list = service->othersList;
133     }
134
135     if(-1 == tmp->currentRow()) {
136         QMessageBox::information(this, QString("No selection"), QString("Please select a file from the list"));
137         return;
138     }
139     GoogleDocument *doc = list[tmp->currentRow()];
140     QString format = "";
141     if("presentation" == doc->documentType()) {
142         format = ".ppt";
143     }
144     else if("document" == doc->documentType()) {
145         format = ".odt";
146     }
147     else if("spreadsheet" == doc->documentType()) {
148         format = ".ods";
149     }
150     QString filename = QFileDialog::getSaveFileName(this,"Save File",QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation) + "/" + doc->title() + format, "(*" + format + ")");
151     if("" == filename)
152         return;
153     service->downloadDocument(doc, new QString(filename));
154     ui->listTab->setEnabled(false);
155     ui->uploadButton->setEnabled(false);
156     ui->downloadButton->setEnabled(false);
157     ui->refreshButton->setEnabled(false);
158     ui->downloadProgressBar->setVisible(true);
159 }
160
161 void googleListDialog::uploadButtonClickedSlot()
162 {
163     googleUploadDialog *ud = new googleUploadDialog(service, this);
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         QMessageBox::information(this, "Download done", "The file has finished downloading");
178     else
179         QMessageBox::information(this, tr("Error"), tr("An error occured while downloading the document"));
180 }
181
182 void googleListDialog::updateProgressBar(qint64 bytesDone, qint64 bytesTotal)
183 {
184     int value = (bytesDone * 100) / bytesTotal;
185     ui->downloadProgressBar->setValue(value);
186 }