2 * Copyright (c) 2010 Kaushal M <kshlmster@gmail.com>
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.
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.
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.
19 #include "googlelistdialog.h"
20 #include "ui_filelistdialog.h"
22 #include "googledocument.h"
23 #include "googleuploaddialog.h"
25 #include <QMessageBox>
26 #include <QFileDialog>
27 #include <QDesktopServices>
29 googleListDialog::googleListDialog(GoogleDocumentService *service, QWidget *parent):
31 ui(new Ui::fileListDialog)
34 this->setWindowTitle("Google Docs");
35 this->service = service;
36 ui->listTab->setCurrentIndex(0);
37 ui->downloadProgressBar->setVisible(false);
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)));
48 void googleListDialog::changeEvent(QEvent *e)
50 QDialog::changeEvent(e);
52 case QEvent::LanguageChange:
53 ui->retranslateUi(this);
60 void googleListDialog::refreshList()
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();
72 void googleListDialog::fillList(bool status)
75 ui->documentList->clear();
76 ui->presentationList->clear();
77 ui->spreadsheetList->clear();
78 ui->othersList->clear();
80 QList<GoogleDocument *> list;
82 list = service->documentList;
83 foreach(GoogleDocument *i, list) {
84 ui->documentList->addItem(i->title());
87 list = service->presentationList;
88 foreach(GoogleDocument *i, list) {
89 ui->presentationList->addItem(i->title());
92 list = service->spreadsheetList;
93 foreach(GoogleDocument *i, list) {
94 ui->spreadsheetList->addItem(i->title());
97 list = service->othersList;
98 foreach(GoogleDocument *i, list) {
99 ui->othersList->addItem(i->title());
103 QMessageBox::information(this, tr("Error"), tr("An error occured while fetching document list"));
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);
113 void googleListDialog::downloadButtonClickedSlot()
115 QList<GoogleDocument *> list;
116 QListWidget *tmp = 0;
117 switch(ui->listTab->currentIndex()) {
119 tmp = ui->documentList;
120 list = service->documentList;
123 tmp = ui->presentationList;
124 list = service->presentationList;
127 tmp = ui->spreadsheetList;
128 list = service->spreadsheetList;
131 tmp = ui->othersList;
132 list = service->othersList;
135 if(-1 == tmp->currentRow()) {
136 QMessageBox::information(this, QString("No selection"), QString("Please select a file from the list"));
139 GoogleDocument *doc = list[tmp->currentRow()];
141 if("presentation" == doc->documentType()) {
144 else if("document" == doc->documentType()) {
147 else if("spreadsheet" == doc->documentType()) {
150 QString filename = QFileDialog::getSaveFileName(this,"Save File",QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation) + "/" + doc->title() + format, "(*" + format + ")");
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);
161 void googleListDialog::uploadButtonClickedSlot()
163 googleUploadDialog *ud = new googleUploadDialog(service, this);
165 connect(ud, SIGNAL(accepted()), this, SLOT(refreshList()));
168 void googleListDialog::downloadDoneSlot(bool status)
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);
177 QMessageBox::information(this, "Download done", "The file has finished downloading");
179 QMessageBox::information(this, tr("Error"), tr("An error occured while downloading the document"));
182 void googleListDialog::updateProgressBar(qint64 bytesDone, qint64 bytesTotal)
184 int value = (bytesDone * 100) / bytesTotal;
185 ui->downloadProgressBar->setValue(value);