]> git.sur5r.net Git - minitube/blob - src/appwidget.cpp
New upstream version 3.4.1
[minitube] / src / appwidget.cpp
1 #include "appwidget.h"
2 #include "constants.h"
3 #include "http.h"
4 #ifdef APP_EXTRA
5 #include "updatedialog.h"
6 #endif
7
8 AppsWidget::AppsWidget(QWidget *parent) : QWidget(parent) {
9     const int padding = 30;
10
11     QBoxLayout *layout = new QHBoxLayout(this);
12     layout->setMargin(padding);
13     layout->setSpacing(padding*2);
14     layout->setAlignment(Qt::AlignCenter);
15
16 #ifdef APP_MAC
17     const QString ext = "dmg";
18 #elif defined APP_WIN
19     const QString ext = "exe";
20 #else
21     const QString ext = "deb";
22 #endif
23
24 #ifdef APP_MAC
25     setupApp("Sofa", "sofa." + ext);
26 #endif
27     setupApp("Finetune", "finetune." + ext);
28     setupApp("Musictube", "musictube." + ext);
29     setupApp("Musique", "musique." + ext);
30 }
31
32 void AppsWidget::setupApp(const QString &name, const QString &code) {
33     AppWidget *w = new AppWidget(name, code);
34     layout()->addWidget(w);
35 }
36
37 void AppsWidget::paintEvent(QPaintEvent *e) {
38     Q_UNUSED(e);
39     QStyleOption o;
40     o.initFrom(this);
41     QPainter p(this);
42     style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
43 }
44
45 AppWidget::AppWidget(const QString &name, const QString &code, QWidget *parent) : QWidget(parent), icon(0), name(name), downloadButton(0) {
46     const QString unixName = code.left(code.lastIndexOf('.'));
47     const QString baseUrl = QLatin1String("https://") + Constants::ORG_DOMAIN;
48     const QString filesUrl = baseUrl + QLatin1String("/files/");
49     url = filesUrl + unixName + QLatin1String("/") + code;
50     webPage = baseUrl + QLatin1String("/") +  unixName;
51
52     QBoxLayout *layout = new QVBoxLayout(this);
53     layout->setMargin(0);
54     layout->setAlignment(Qt::AlignHCenter);
55
56     icon = new QLabel();
57     icon->setMinimumHeight(128);
58     layout->addWidget(icon);
59     const QString iconUrl = filesUrl + QLatin1String("products/") + unixName + QLatin1String(".png");
60     QObject *reply = Http::instance().get(iconUrl);
61     connect(reply, SIGNAL(data(QByteArray)), SLOT(iconDownloaded(QByteArray)));
62
63     QLabel *appTitle = new QLabel(name);
64     appTitle->setAlignment(Qt::AlignHCenter);
65     layout->addWidget(appTitle);
66
67 #ifdef APP_EXTRA
68 #if !defined(APP_UBUNTU) && !defined(APP_MAC_STORE)
69     downloadButton = new QPushButton(tr("Download"));
70     downloadButton->setAttribute(Qt::WA_MacSmallSize);
71     downloadButton->setCursor(Qt::ArrowCursor);
72     QSizePolicy sp = downloadButton->sizePolicy();
73     sp.setHorizontalPolicy(QSizePolicy::Fixed);
74     sp.setRetainSizeWhenHidden(true);
75     downloadButton->setSizePolicy(sp);
76     connect(downloadButton, SIGNAL(clicked(bool)), SLOT(downloadApp()));
77     layout->addWidget(downloadButton, Qt::AlignHCenter);
78     layout->setAlignment(downloadButton, Qt::AlignHCenter);
79     downloadButton->hide();
80 #endif
81 #endif
82
83     setCursor(Qt::PointingHandCursor);
84 }
85
86 void AppWidget::enterEvent(QEvent *e) {
87     Q_UNUSED(e);
88     if (downloadButton) downloadButton->show();
89 }
90
91 void AppWidget::leaveEvent(QEvent *e) {
92     Q_UNUSED(e);
93     if (downloadButton) downloadButton->hide();
94 }
95
96 void AppWidget::mouseReleaseEvent(QMouseEvent *e) {
97     if (e->button() == Qt::LeftButton) {
98         QDesktopServices::openUrl(webPage);
99     }
100 }
101
102 void AppWidget::iconDownloaded(const QByteArray &bytes) {
103     QPixmap pixmap;
104     pixmap.loadFromData(bytes, "PNG");
105     icon->setPixmap(pixmap);
106 }
107
108 void AppWidget::downloadApp() {
109 #ifdef APP_EXTRA
110     if (!icon) return;
111     UpdateDialog *dialog = new UpdateDialog(icon->pixmap(), name, QString(), url, this);
112     dialog->downloadUpdate();
113     dialog->show();
114 #endif
115 }