store path to conference map in database
[toast/confclerk.git] / src / gui / conferenceeditor.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  *
4  * This file is part of fosdem-schedule.
5  *
6  * fosdem-schedule is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * fosdem-schedule is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "conferenceeditor.h"
20
21 #include "conferencemodel.h"
22 #include "urlinputdialog.h"
23 #include "mapwindow.h"
24 #include "errormessage.h"
25
26 #include <QInputDialog>
27 #include <QItemSelectionModel>
28 #include <QFileDialog>
29 #include <QMessageBox>
30
31 ConferenceEditor::ConferenceEditor(ConferenceModel* model, QWidget* parent)
32 : QDialog(parent)
33 , model(model)
34 , selected_id(-1)
35 {
36     setupUi(this);
37     progressBar->hide();
38
39     confView->setModel(model);
40
41     QItemSelectionModel* confViewSelection = new QItemSelectionModel(model, this);
42     confView->setSelectionModel(confViewSelection);
43
44     connect(confViewSelection, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
45                                 SLOT(itemSelected(const QModelIndex&, const QModelIndex&)));
46     connect(this, SIGNAL(wantCurrent(const QModelIndex&, QItemSelectionModel::SelectionFlags)),
47             confViewSelection, SLOT(setCurrentIndex(const QModelIndex&, QItemSelectionModel::SelectionFlags)));
48     connect(addBtn, SIGNAL(clicked()), SLOT(addClicked()));
49     connect(removeBtn, SIGNAL(clicked()), SLOT(removeClicked()));
50     connect(changeUrl, SIGNAL(clicked()), SLOT(changeUrlClicked()));
51     connect(refreshBtn, SIGNAL(clicked()), SLOT(refreshClicked()));
52     connect(showMapButton, SIGNAL(clicked()), SLOT(conferenceMapClicked()));
53     connect(buttonBox, SIGNAL(rejected()), SLOT(close()));
54
55     // it's OK to emit selection signals here
56     // because they are not yet connected to anybody
57     int active_id = Conference::activeConference();
58     if (active_id > 0) {
59         emit wantCurrent(model->indexFromId(active_id), QItemSelectionModel::SelectCurrent);
60     } else {
61         itemSelected(QModelIndex(), QModelIndex());
62     }
63 }
64
65 void ConferenceEditor::conferenceRemoved()
66 {
67     if (model->rowCount() > 0) {
68         emit wantCurrent(model->index(0, 0), QItemSelectionModel::SelectCurrent);
69     } else {
70         itemSelected(QModelIndex(), QModelIndex());
71     }
72 }
73
74 void ConferenceEditor::itemSelected(const QModelIndex& current, const QModelIndex& previous)
75 {
76     // TODO: fill all required fields
77     Q_UNUSED(previous);
78     if (!current.isValid()) {
79         selected_id = -1;
80
81         emit noneConferenceSelected();
82
83         conferenceInfo->setCurrentIndex(1);
84         removeBtn->hide();
85     } else {
86         const Conference& conf = model->conferenceFromIndex(current);
87         selected_id = conf.id();
88
89         emit haveConferenceSelected(selected_id);
90
91         conferenceTitle->setText(conf.title());
92         conferenceSubtitle->setText(conf.subtitle());
93         conferenceWhere->setText(conf.city() + ", " + conf.venue());
94         conferenceWhen->setText(
95                 conf.start().toString("dd-MM-yyyy")
96                 + ", " +
97                 conf.end().toString("dd-MM-yyyy"));
98
99         QString map = conf.map();
100         if (map.isEmpty()) {
101             showMapButton->hide();
102         } else {
103             showMapButton->show();
104         }
105
106         conferenceInfo->setCurrentIndex(0);
107         removeBtn->show();
108     }
109 }
110
111 void ConferenceEditor::addClicked()
112 {
113     UrlInputDialog url_input(this);
114     switch (url_input.exec()) {
115     case UrlInputDialog::HaveUrl: emit haveConferenceUrl(url_input.url()); break;
116     case UrlInputDialog::HaveFile: emit haveConferenceFile(url_input.url()); break;
117     case UrlInputDialog::Cancel: return;
118     }
119 }
120
121 void ConferenceEditor::removeClicked()
122 {
123     if (selected_id < 0) {
124         // TODO: disable it when none is selected
125         return;
126     }
127
128     QMessageBox::StandardButton answer = 
129         QMessageBox::question(0
130             , "Deletion confirmation"
131             , QString("Really delete the %1 conference").arg(Conference::getById(selected_id).title())
132             , QMessageBox::Yes | QMessageBox::No
133             , QMessageBox::No);
134
135     if (answer == QMessageBox::Yes) {
136         emit removeConferenceRequested(selected_id);
137     }
138 }
139
140 void ConferenceEditor::changeUrlClicked()
141 {
142     if (selected_id < 0) {
143         return;
144     }
145     const Conference& selected = Conference::getById(selected_id);
146
147     bool ok;
148     QString url = QInputDialog::getText(this, "URL Input", "Enter schedule URL", QLineEdit::Normal, selected.url(), &ok);
149
150     if (ok) {
151         emit changeUrlRequested(selected_id, url);
152     }
153 }
154
155 void ConferenceEditor::refreshClicked()
156 {
157     if (selected_id < 0) {
158         return;
159     }
160     const Conference& selected = Conference::getById(selected_id);
161
162     QString url = selected.url();
163
164     if (!url.isEmpty()) {
165         emit haveConferenceUrl(url);
166     } else {
167         static const QString format("Schedule URL for %1 is not set. Enter the schedule URL:");
168         bool ok;
169         QString url = QInputDialog::getText(this, "URL Input", format.arg(selected.title()), QLineEdit::Normal, QString(), &ok);
170
171         if (ok) {
172             // first save it, to remain if fetch fails
173             emit changeUrlRequested(selected_id, url);
174             // then fetch
175             emit haveConferenceUrl(url);
176         }
177     }
178 }
179
180 void ConferenceEditor::importStarted()
181 {
182     addBtn->hide();
183     removeBtn->hide();
184     buttons->layout()->removeItem(buttonsSpacer);
185     progressBar->setValue(0);
186     progressBar->show();
187
188     QApplication::processEvents();
189 }
190
191 void ConferenceEditor::showParsingProgress(int progress)
192 {
193     progressBar->setValue(progress);
194
195     QApplication::processEvents();
196 }
197
198 void ConferenceEditor::importFinished(const QString& title)
199 {
200     qDebug() << __PRETTY_FUNCTION__ << title;
201     addBtn->show();
202     // removeItem should be shown later, but it takes some time,
203     // and not looks good
204     // anyway it will be shown a bit later
205     removeBtn->show();
206     buttons->layout()->addItem(buttonsSpacer);
207     progressBar->hide();
208
209     QApplication::processEvents();
210
211     int num = model->rowCount();
212     for (int i = 0; i < num; i++) {
213         QModelIndex item = model->index(i, 0);
214         if (model->data(item) == title) {
215             emit wantCurrent(item, QItemSelectionModel::SelectCurrent);
216             return;
217         }
218     }
219     itemSelected(QModelIndex(), QModelIndex());
220 }
221
222 void ConferenceEditor::conferenceMapClicked()
223 {
224     Conference conf = Conference::getById(selected_id);
225     QString mapPath = conf.map();
226     if(mapPath.isEmpty() or !QFile::exists(mapPath)) {
227         error_message("Map is not available");
228         return;
229     }
230
231     QString roomName;
232
233     QPixmap map(mapPath);
234     MapWindow window(map,roomName,this);
235     window.exec();
236 }