Initial Commit.
[onlineservices] / 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 <QMessageBox>
26 #include <QFileDialog>
27 #include <QDesktopServices>
28 #include <QListWidgetItem>
29 #include <QMenu>
30 #include <QMouseEvent>
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->listTab->setEnabled(false);
60     ui->downloadButton->setEnabled(false);
61     ui->uploadButton->setEnabled(false);
62     ui->refreshButton->setEnabled(false);
63     service->listDocuments();
64 }
65
66 void slideshareListDialog::changeEvent(QEvent *e)
67 {
68     QDialog::changeEvent(e);
69     switch (e->type()) {
70     case QEvent::LanguageChange:
71         ui->retranslateUi(this);
72         break;
73     default:
74         break;
75     }
76 }
77
78 void slideshareListDialog::downloadButtonClickedSlot()
79 {
80     QListWidget *tmp;
81     QList<SlideShareDocument> list;
82     if(0 == ui->listTab->currentIndex())
83     {
84         tmp = ui->documentList;
85         list = service->textDocList;
86     }
87     if(1 == ui->listTab->currentIndex())
88     {
89         tmp = ui->presentationList;
90         list = service->presentationList;
91     }
92     if(2 == ui->listTab->currentIndex())
93     {
94         tmp = ui->spreadsheetList;
95         list = service->spreadsheetList;
96     }
97
98     if(tmp->currentRow() == -1)
99     {
100         QMessageBox::information(this, QString("No selection"), QString("Please select a file from the list"));
101         return;
102     }
103
104     SlideShareDocument p = list[tmp->currentRow()];
105     QString durl = p.downloadUrl;
106
107     QString saveFileName = QFileDialog::getSaveFileName(this, "Save file", QDesktopServices::storageLocation(QDesktopServices::HomeLocation).append("/").append(p.title).append(p.format), QString("(*.").append(p.format).append(")"));
108     qDebug() << saveFileName << endl;
109     if("" == saveFileName)
110     {
111         return;
112     }
113     service->setSaveFileName(new QString(saveFileName));
114
115     service->download(&durl);
116     ui->listTab->setEnabled(false);
117     ui->downloadButton->setEnabled(false);
118     ui->uploadButton->setEnabled(false);
119     ui->refreshButton->setEnabled(false);
120     ui->downloadProgressBar->setVisible(true);
121 }
122
123 void slideshareListDialog::uploadButtonClickedSlot()
124 {
125     slideshareUploadDialog *ud = new slideshareUploadDialog(service, this);
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     {
142         SlideShareDocument j = *i;
143         QListWidgetItem *k = new QListWidgetItem(j.title);
144         ui->documentList->addItem(k);
145     }
146
147     list = service->presentationList;
148     for(i = list.begin(); i != list.end(); ++i)
149     {
150         SlideShareDocument j = *i;
151         QListWidgetItem *k = new QListWidgetItem(j.title);
152         ui->presentationList->addItem(k);
153     }
154
155     list = service->spreadsheetList;
156     for(i = list.begin(); i != list.end(); ++i)
157     {
158         SlideShareDocument j = *i;
159         QListWidgetItem *k = new QListWidgetItem(j.title);
160         ui->spreadsheetList->addItem(k);
161     }
162
163     ui->listTab->setEnabled(true);
164     ui->downloadButton->setEnabled(true);
165     ui->uploadButton->setEnabled(true);
166     ui->refreshButton->setEnabled(true);
167 }
168
169 void slideshareListDialog::updateProgressBar(qint64 done, qint64 total)
170 {
171     int value = (done * 100/total);
172     ui->downloadProgressBar->setValue((int)value);
173 }
174
175 void slideshareListDialog::downloadDoneSlot()
176 {
177     ui->downloadProgressBar->setVisible(false);
178     ui->downloadProgressBar->setValue(0);
179     ui->listTab->setEnabled(true);
180     ui->downloadButton->setEnabled(true);
181     ui->refreshButton->setEnabled(true);
182     ui->uploadButton->setEnabled(true);
183     QMessageBox::information(this, "Download done", "The file has finished downloading");
184 }
185
186 bool slideshareListDialog::eventFilter(QObject *obj, QEvent *event)
187 {
188     if(event->type() == QEvent::ContextMenu)
189     {
190         QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
191         QMenu *menu = new QMenu(this);
192         menu->addAction(new QAction("CLICK", this));
193         menu->exec(mouseEvent->globalPos());
194         return false;
195     }
196     return true;
197 }
198