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