Added KOfficemeego dependency
[froff-onlinedoc] / slidesharelistdialog.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 "slidesharelistdialog.h"
20 #include "ui_filelistdialog.h"
21
22 #include "slideshareuploaddialog.h"
23 #include "slideshareDocument.h"
24
25 #include <QFileDialog>
26 #include <QDesktopServices>
27 #include <QListWidgetItem>
28 #include <QMenu>
29 #include <QMouseEvent>
30 #include <QMaemo5InformationBox>
31
32 slideshareListDialog::slideshareListDialog(SlideShare *s, QWidget *parent):
33     QDialog(parent),
34     ui(new Ui::fileListDialog)
35 {
36     ui->setupUi(this);
37     this->setWindowTitle("SlideShare");
38     ui->downloadProgressBar->setVisible(false);
39     this->service = s;
40     ui->listTab->setCurrentIndex(1);
41     ui->listTab->removeTab(3);
42     connect(ui->uploadButton, SIGNAL(clicked()), this, SLOT(uploadButtonClickedSlot()));
43     connect(ui->downloadButton, SIGNAL(clicked()), this, SLOT(downloadButtonClickedSlot()));
44     connect(ui->refreshButton, SIGNAL(clicked()), this, SLOT(refreshList()));
45     connect(service, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(updateProgressBar(qint64, qint64)));
46     connect(service, SIGNAL(downloadDone()), this, SLOT(downloadDoneSlot()));
47     connect(service, SIGNAL(listDone()), this, SLOT(fillList()));
48
49     refreshList();
50 }
51
52 slideshareListDialog::~slideshareListDialog()
53 {
54     delete ui;
55 }
56
57 void slideshareListDialog::refreshList()
58 {
59     ui->downloadProgressBar->setMaximum(0);
60     ui->downloadProgressBar->setVisible(true);
61     ui->listTab->setEnabled(false);
62     ui->downloadButton->setEnabled(false);
63     ui->uploadButton->setEnabled(false);
64     ui->refreshButton->setEnabled(false);
65     service->listDocuments();
66 }
67
68 void slideshareListDialog::changeEvent(QEvent *e)
69 {
70     QDialog::changeEvent(e);
71     switch(e->type()) {
72     case QEvent::LanguageChange:
73         ui->retranslateUi(this);
74         break;
75     default:
76         break;
77     }
78 }
79
80 void slideshareListDialog::downloadButtonClickedSlot()
81 {
82     QListWidget *tmp;
83     QList<SlideShareDocument> list;
84     if(0 == ui->listTab->currentIndex()) {
85         tmp = ui->documentList;
86         list = service->textDocList;
87     }
88     if(1 == ui->listTab->currentIndex()) {
89         tmp = ui->presentationList;
90         list = service->presentationList;
91     }
92     if(2 == ui->listTab->currentIndex()) {
93         tmp = ui->spreadsheetList;
94         list = service->spreadsheetList;
95     }
96
97     if(tmp->currentRow() == -1) {
98         QMaemo5InformationBox::information(this, "Select a file from the list to download", QMaemo5InformationBox::DefaultTimeout);
99         return;
100     }
101
102     SlideShareDocument p = list[tmp->currentRow()];
103     QString durl = p.downloadUrl;
104
105     QString saveFileName = QFileDialog::getSaveFileName(this, "Save file", QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation).append("/").append(p.title).append(".").append(p.format), QString("(*.").append(p.format).append(")"));
106     qDebug() << saveFileName << endl;
107     if("" == saveFileName) {
108         return;
109     }
110     service->setSaveFileName(new QString(saveFileName));
111
112     service->download(&durl);
113     ui->listTab->setEnabled(false);
114     ui->downloadButton->setEnabled(false);
115     ui->uploadButton->setEnabled(false);
116     ui->refreshButton->setEnabled(false);
117     ui->downloadProgressBar->setVisible(true);
118 }
119
120 void slideshareListDialog::uploadButtonClickedSlot()
121 {
122     slideshareUploadDialog *ud = new slideshareUploadDialog(service, this);
123     if("" != openDocPath) {
124         ud->setOpenDoc(openDocPath);
125     }
126     ud->show();
127     connect(ud, SIGNAL(accepted()), this, SLOT(refreshList()));
128 }
129
130 void slideshareListDialog::fillList()
131 {
132     ui->documentList->clear();
133     ui->presentationList->clear();
134     ui->spreadsheetList->clear();
135
136     QList<SlideShareDocument>  list;
137     QList<SlideShareDocument>::iterator i;
138
139     list = service->textDocList;
140     for(i = list.begin(); i != list.end(); ++i) {
141         SlideShareDocument j = *i;
142         QListWidgetItem *k = new QListWidgetItem(j.title);
143         ui->documentList->addItem(k);
144     }
145
146     list = service->presentationList;
147     for(i = list.begin(); i != list.end(); ++i) {
148         SlideShareDocument j = *i;
149         QListWidgetItem *k = new QListWidgetItem(j.title);
150         ui->presentationList->addItem(k);
151     }
152
153     list = service->spreadsheetList;
154     for(i = list.begin(); i != list.end(); ++i) {
155         SlideShareDocument j = *i;
156         QListWidgetItem *k = new QListWidgetItem(j.title);
157         ui->spreadsheetList->addItem(k);
158     }
159     ui->downloadProgressBar->setMaximum(100);
160     ui->downloadProgressBar->setVisible(false);
161     ui->listTab->setEnabled(true);
162     ui->downloadButton->setEnabled(true);
163     ui->uploadButton->setEnabled(true);
164     ui->refreshButton->setEnabled(true);
165 }
166
167 void slideshareListDialog::updateProgressBar(qint64 done, qint64 total)
168 {
169     int value = (done * 100 / total);
170     ui->downloadProgressBar->setValue((int)value);
171 }
172
173 void slideshareListDialog::downloadDoneSlot()
174 {
175     ui->downloadProgressBar->setVisible(false);
176     ui->downloadProgressBar->setValue(0);
177     ui->listTab->setEnabled(true);
178     ui->downloadButton->setEnabled(true);
179     ui->refreshButton->setEnabled(true);
180     ui->uploadButton->setEnabled(true);
181     QMaemo5InformationBox::information(this, "The file has finished downloading", QMaemo5InformationBox::DefaultTimeout);
182 }
183
184 void slideshareListDialog::setOpenDoc(const QString & openDocPath)
185 {
186     this->openDocPath = openDocPath;
187 }
188
189 bool slideshareListDialog::eventFilter(QObject *obj, QEvent *event)
190 {
191     if(event->type() == QEvent::ContextMenu) {
192         QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
193         QMenu *menu = new QMenu(this);
194         menu->addAction(new QAction("CLICK", this));
195         menu->exec(mouseEvent->globalPos());
196         return false;
197     }
198     return true;
199 }
200