10 #include <WelcomeWidget.h>
11 #include <ui_WelcomeWidget.h>
12 #include <core/modules/ProjectControl.h>
22 ui->tabWidget->installEventFilter(
new ResizeFilter(ui->tabWidget));
24 connect(ui->pushButton_OpenProject, SIGNAL(pressed()),
27 connect(ui->pushButton_NewProject, SIGNAL(pressed()),
30 ui->pushButton_DownloadApp->setEnabled(
false);
32 connect(&httpNotifyMessages, SIGNAL(readyRead(
const QHttpResponseHeader &)),
33 this, SLOT(notifyMessagesReadData(
const QHttpResponseHeader &)));
37 connect(&http, SIGNAL(readyRead(
const QHttpResponseHeader &)),
38 this, SLOT(readData(
const QHttpResponseHeader &)));
40 connect(&http, SIGNAL(requestFinished(
int,
bool)),
41 this, SLOT(finishRSSLoad(
int,
bool)));
43 connect(ui->pushButton_DownloadApp, SIGNAL(pressed()),
44 this, SLOT(downloadApplication()));
46 connect(ui->listWidget_NCLClub, SIGNAL(currentRowChanged(
int)),
47 this, SLOT(changeCurrentItem(
int)));
49 progressDialog =
new QProgressDialog(
this);
50 connect(progressDialog, SIGNAL(canceled()),
this, SLOT(cancelDownload()));
52 isDownloading =
false;
56 ui->tabWidget->removeTab(2);
59 #ifdef WITH_TEST_VERSION_MESSAGE
60 #define LABEL_BUILD "This is an ALPHA version. Version: " \
61 NCLCOMPOSER_GUI_VERSION " Build Date and time:" BUILD_DATE
63 ui->label_BuildMessage->setText(LABEL_BUILD);
64 ui->label_BuildMessage->setStyleSheet(
"color:white; font-size:13px; "
65 "text-align: right;");
69 ui->tabWidget->removeTab(1);
72 ui->frame_Tips->setVisible(
false);
75 ui->labelNotifyMessage->setVisible(
false);
76 updateNotifyMessages();
84 void WelcomeWidget::updateRecentProjects(QStringList recentProjects)
86 foreach (QCommandLinkButton *button,
87 ui->frame_RecentProjects->findChildren<QCommandLinkButton*>())
90 button->deleteLater();
93 for(
int i = 0; i < recentProjects.size() && i < MAX_RECENT_PROJECTS; i++)
95 QString file = recentProjects.at(i);
96 QCommandLinkButton *button =
new QCommandLinkButton(
97 file.mid(file.lastIndexOf(
"/")+1), file, ui->frame_RecentProjects);
98 button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
99 button->setToolTip(file);
100 connect(button, SIGNAL(pressed()),
this, SLOT(sendRecentProjectClicked()));
102 ui->frame_RecentProjects->layout()->addWidget(button);
106 void WelcomeWidget::updateNotifyMessages()
108 QUrl url = QUrl::fromUserInput(NCL_COMPOSER_NOTIFY_URL);
109 httpNotifyMessages.setHost(url.host());
110 connectionId = httpNotifyMessages.get(url.path());
113 void WelcomeWidget::notifyMessagesReadData(
const QHttpResponseHeader &resp)
115 if (resp.statusCode() != 200)
117 httpNotifyMessages.abort();
121 int min_message_id_to_show = DEFAULT_MIN_MESSAGE_ID_TO_SHOW;
122 int max_notify_messages = DEFAULT_MAX_NOTIFY_MESSAGES;
125 GlobalSettings settings;
126 settings.beginGroup(
"notify_system");
127 if(settings.contains(
"min_message_id_to_show"))
128 min_message_id_to_show =
129 qMax( settings.value(
"min_message_id_to_show").toInt(),
130 min_message_id_to_show);
132 settings.setValue(
"min_message_id_to_show", min_message_id_to_show);
134 if(settings.contains(
"max_notify_messages"))
135 max_notify_messages = settings.value(
"max_notify_messages").toInt();
137 settings.setValue(
"max_notify_messages", max_notify_messages);
139 QByteArray bytes = httpNotifyMessages.readAll();
142 QStringList messages = QString(bytes).split(
"\n");
143 QString messageToShow =
"";
145 for(
int i = 0; i < messages.size(); i++)
147 if( i > max_notify_messages - 1 )
break;
148 QStringList msgSplittedId = messages[i].split(
"\t");
150 if(msgSplittedId.size() >= 2)
152 if( msgSplittedId[0].toInt() >= min_message_id_to_show )
153 messageToShow += msgSplittedId[1];
159 if(messageToShow.size())
161 ui->labelNotifyMessage->setText(messageToShow);
162 ui->labelNotifyMessage->setVisible(
true);
168 void WelcomeWidget::sendRecentProjectClicked()
170 QCommandLinkButton *button =
171 static_cast<QCommandLinkButton*
> (QObject::sender());
175 emit userPressedRecentProject(button->description());
180 void WelcomeWidget::loadRSS()
184 isImageEnclosure =
true;
190 QUrl url = QUrl::fromUserInput(NCL_CLUB_URL);
192 http.setHost(url.host());
193 connectionId = http.get(url.path());
196 void WelcomeWidget::readData(
const QHttpResponseHeader &resp)
198 if (resp.statusCode() != 200)
204 QByteArray bytes = http.readAll();
205 xmlReader.addData(bytes);
210 void WelcomeWidget::parseXml()
214 bool readingItem =
false;
216 while (!xmlReader.atEnd())
218 xmlReader.readNext();
220 if (xmlReader.isStartElement())
222 if (xmlReader.name() ==
"item")
227 currentTitle.clear();
231 else if(xmlReader.name() ==
"enclosure")
235 currentImg = xmlReader.attributes().value(
"url").toString();
236 imgSrc.push_back(currentImg);
237 isImageEnclosure =
false;
241 isImageEnclosure =
true;
242 currentDownloadUrl = xmlReader.attributes().value(
"url").toString();
243 downloadUrl.push_back(currentDownloadUrl);
247 currentTag = xmlReader.name().toString();
249 else if (xmlReader.isEndElement())
251 if (xmlReader.name() ==
"item")
256 ui->listWidget_NCLClub->addItem(currentTitle);
257 description.push_back(currentDesc);
262 currentTitle.clear();
270 else if (xmlReader.isCharacters() && !xmlReader.isWhitespace())
272 if (currentTag ==
"title" && readingItem)
273 currentTitle += xmlReader.text().toString();
274 else if (currentTag ==
"link")
275 currentLink += xmlReader.text().toString();
276 else if (currentTag ==
"pubDate")
277 currentDate += xmlReader.text().toString();
278 else if(currentTag ==
"description")
279 currentDesc += xmlReader.text().toString();
283 if (xmlReader.error() &&
284 xmlReader.error() != QXmlStreamReader::PrematureEndOfDocumentError)
286 qWarning() <<
"XML ERROR:"
287 << xmlReader.lineNumber() <<
": " << xmlReader.errorString();
293 void WelcomeWidget::changeCurrentItem(
int item)
295 currentNCLClubItem = item;
298 ui->pushButton_DownloadApp->setEnabled(
true);
300 QString html =
"<html><body><img src=\"";
301 html += imgSrc[item];
303 html += description.at(item);
304 html +=
"</body></html>";
305 ui->webView->setHtml(html);
308 void WelcomeWidget::finishRSSLoad(
int connectionId,
bool error)
310 qDebug() <<
"finishRSSLoad(" << connectionId <<
"," << error <<
");";
313 if(this->connectionId == connectionId)
315 QCommandLinkButton *button =
316 new QCommandLinkButton(
this);
317 button->setIconSize(QSize(0,0));
319 ui->listWidget_NCLClub->addItem(tr(
"Connection to NCL Club has failed."));
324 void WelcomeWidget::downloadApplication()
326 QString urlStr = downloadUrl[currentNCLClubItem];
329 ui->pushButton_DownloadApp->setEnabled(
false);
330 ui->pushButton_DownloadApp->setText(tr(
"Downloading application..."));
332 progressDialog->show();
335 void WelcomeWidget::downloadFile()
337 isDownloading =
true;
338 QFileInfo fileInfo(url.path());
339 QString fileName = QDir::tempPath() + QDir::separator() + fileInfo.fileName();
340 if (fileName.isEmpty())
341 fileName =
"index.html";
343 if (QFile::exists(fileName)) {
344 if (QMessageBox::question(
this, tr(
"Application from NCL Club"),
345 tr(
"There already exists a file called %1 in "
346 "the current directory. Overwrite?").arg(fileName),
347 QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
350 QFile::remove(fileName);
353 file =
new QFile(fileName);
354 if (!file->open(QIODevice::WriteOnly)) {
355 QMessageBox::information(
this, tr(
"HTTP"),
356 tr(
"Unable to save the file %1: %2.")
357 .arg(fileName).arg(file->errorString()));
363 progressDialog->setWindowTitle(tr(
"Application from NCL Club"));
364 progressDialog->setLabelText(tr(
"Downloading %1.").arg(fileName));
368 httpRequestAborted =
false;
372 void WelcomeWidget::startRequest(
const QUrl &url)
374 reply = qnam.get(QNetworkRequest(url));
375 connect(reply, SIGNAL(finished()),
376 this, SLOT(httpFinished()));
377 connect(reply, SIGNAL(readyRead()),
378 this, SLOT(httpReadyRead()));
379 connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
380 this, SLOT(updateDataReadProgress(qint64,qint64)));
383 void WelcomeWidget::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
385 if (httpRequestAborted)
388 progressDialog->setMaximum(totalBytes);
389 if(totalBytes > bytesRead)
390 progressDialog->setValue(bytesRead);
393 void WelcomeWidget::cancelDownload()
396 httpRequestAborted =
true;
400 isDownloading =
false;
401 ui->pushButton_DownloadApp->setEnabled(
true);
404 void WelcomeWidget::httpFinished()
406 if (httpRequestAborted) {
413 reply->deleteLater();
414 progressDialog->hide();
420 isDownloading =
false;
421 ui->pushButton_DownloadApp->setEnabled(
true);
422 ui->pushButton_DownloadApp->setText(tr(
"Download and Import this application"));
424 QFileInfo fileInfo(*file);
426 QString filename = QFileDialog::getSaveFileName(
428 tr(
"Choose the name of the new project to be created"),
430 tr(
"NCL Composer Projects (*.cpr)") );
432 if( !filename.isNull())
434 if(!filename.endsWith(
".cpr"))
435 filename = filename + QString(
".cpr");
439 qDebug() <<
"Filename is empty. A project will not be created!" << endl;
443 QFileInfo fileInfo2(filename);
444 projectName = filename;
446 extract(fileInfo.absoluteFilePath(), fileInfo2.absoluteDir().absolutePath());
449 reply->deleteLater();
455 void WelcomeWidget::httpReadyRead()
462 file->write(reply->readAll());
465 bool WelcomeWidget::doExtractCurrentFile( QString extDirPath,
466 QString singleFileName,
472 if (zip.getZipError() != UNZ_OK)
474 qWarning(
"testRead(): zip.close(): %d", zip.getZipError());
478 qDebug() << fileNameToImport << projectName;
479 ProjectControl::getInstance()->importFromDocument( fileNameToImport,
481 progressDialog->hide();
491 zip.setCurrentFile(zipFile.getActualFileName());
492 if (!zip.getCurrentFileInfo(&info))
494 qWarning(
"testRead(): getCurrentFileInfo(): %d\n", zip.getZipError());
498 if (!singleFileName.isEmpty())
499 if (!info.name.contains(singleFileName))
502 if (!zipFile.open(QIODevice::ReadOnly))
504 qWarning(
"testRead(): file.open(): %d", zipFile.getZipError());
508 name = QString(
"%1/%2").arg(extDirPath).arg(zipFile.getActualFileName());
514 if(name.endsWith(QDir::separator()))
519 emit extractNextFile(extDirPath, singleFileName, !zip.goToNextFile());
523 if (zipFile.getZipError() != UNZ_OK)
525 qWarning(
"testRead(): file.getFileName(): %d", zipFile.getZipError());
529 out.setFileName(name);
532 out.open(QIODevice::WriteOnly);
537 while (zipFile.getChar(&c)) out.putChar(c);
542 if(name.endsWith(
".ncl"))
544 fileNameToImport = QFileInfo(out).absoluteFilePath();
547 if (zipFile.getZipError() != UNZ_OK)
549 qWarning(
"testRead(): file.getFileName(): %d", zipFile.getZipError());
553 if (!zipFile.atEnd())
555 qWarning(
"testRead(): read all but not EOF");
561 if (zipFile.getZipError() != UNZ_OK) {
562 qWarning(
"testRead(): file.close(): %d", zipFile.getZipError());
566 emit extractNextFile(extDirPath, singleFileName, !zip.goToNextFile());
571 bool WelcomeWidget::extract(
const QString &filePath,
572 const QString &extDirPath,
573 const QString &singleFileName)
575 zip.setZipName(filePath);
576 zipFile.setZip(&zip);
578 connect(
this, SIGNAL(extractNextFile(QString,QString,
bool)),
579 this, SLOT(doExtractCurrentFile(QString,QString,
bool)),
580 Qt::QueuedConnection);
582 if (!zip.open(QuaZip::mdUnzip))
584 qWarning() << filePath;
585 qWarning(
"testRead(): zip.open(): %d", zip.getZipError());
589 zip.setFileNameCodec(
"IBM866");
591 qWarning(
"%d entries\n", zip.getEntriesCount());
592 qWarning(
"Global comment: %s\n", zip.getComment().toLocal8Bit().constData());
594 this->currentFile = 0;
596 emit extractNextFile(extDirPath, singleFileName, !zip.goToFirstFile());
603 void composer::gui::WelcomeWidget::on_commandLinkButton_29_clicked()
605 QDesktopServices::openUrl(QUrl(
"http://www.telemidia.puc-rio.br/sites/telemidia.puc-rio.br/files/Introduction%20to%20DTV%20and%20to%20Ginga-NCL.pdf"));
608 void composer::gui::WelcomeWidget::on_commandLinkButton_6_clicked()
610 QDesktopServices::openUrl(QUrl(
"http://www.telemidia.puc-rio.br/sites/telemidia.puc-rio.br/files/Curso%20Ginga%20Brasil.zip"));
623 void composer::gui::WelcomeWidget::on_commandLinkButton_10_clicked()
625 QDesktopServices::openUrl(QUrl(
"http://www.telemidia.puc-rio.br/sites/telemidia.puc-rio.br/files/Part%209%20-%20NCL3.0-EC.pdf"));
628 void composer::gui::WelcomeWidget::on_commandLinkButton_11_clicked()
630 QDesktopServices::openUrl(QUrl(
"http://www.telemidia.puc-rio.br/sites/telemidia.puc-rio.br/files/Part%209%20-%20NCL3.0-EC.pdf"));
633 void composer::gui::WelcomeWidget::on_commandLinkButton_31_clicked()
635 QDesktopServices::openUrl(QUrl(
"http://www.ncl.org.br/en/relatoriostecnicos"));
638 void composer::gui::WelcomeWidget::on_commandLinkButton_7_clicked()
640 QDesktopServices::openUrl(QUrl(
"http://composer.telemidia.puc-rio.br"));
643 void composer::gui::WelcomeWidget::on_commandLinkButton_8_clicked()
645 QDesktopServices::openUrl(QUrl(
"http://composer.telemidia.puc-rio.br/doku.php/how_to_create_a_plugin_to_ncl_composer"));
648 void composer::gui::WelcomeWidget::on_commandLinkButton_pressed()
650 QDesktopServices::openUrl(QUrl(
"http://composer.telemidia.puc-rio.br/doku.php/plugins:index"));
653 void composer::gui::WelcomeWidget::on_commandLinkButton_2_pressed()
655 emit userPressedSeeInstalledPlugins();
658 void composer::gui::WelcomeWidget::on_commandLinkButton_3_pressed()
660 QDesktopServices::openUrl(QUrl(
"http://www.telemidia.puc-rio.br/sites/telemidia.puc-rio.br/files/Programando_em_NCL_30.pdf"));