]> git.sur5r.net Git - minitube/blob - src/SearchView.cpp
Imported Upstream version 1.0
[minitube] / src / SearchView.cpp
1 #include "SearchView.h"
2 #include "Constants.h"
3
4 namespace The {
5     QMap<QString, QAction*>* globalActions();
6 }
7
8 static const QString recentKeywordsKey = "recentKeywords";
9 static const int PADDING = 30;
10
11 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
12
13     QFont biggerFont;
14     biggerFont.setPointSize(biggerFont.pointSize()*1.5);
15
16     QFont smallerFont;
17     smallerFont.setPointSize(smallerFont.pointSize()*.85);
18     smallerFont.setBold(true);
19
20     QBoxLayout *mainLayout = new QVBoxLayout();
21     mainLayout->setMargin(0);
22     mainLayout->setSpacing(0);
23
24     // hidden message widget
25     message = new QLabel(this);
26     message->hide();
27     mainLayout->addWidget(message);
28
29     mainLayout->addStretch();
30
31     QBoxLayout *hLayout = new QHBoxLayout();
32     hLayout->setAlignment(Qt::AlignCenter);
33     mainLayout->addLayout(hLayout);
34
35     QLabel *logo = new QLabel(this);
36     logo->setPixmap(QPixmap(":/images/app.png"));
37     hLayout->addWidget(logo, 0, Qt::AlignTop);
38     hLayout->addSpacing(PADDING);
39
40     QVBoxLayout *layout = new QVBoxLayout();
41     layout->setAlignment(Qt::AlignCenter);
42     hLayout->addLayout(layout);
43
44     QLabel *welcomeLabel =
45             new QLabel("<h1>" +
46                        tr("Welcome to <a href='%1'>%2</a>,")
47                        .replace("<a ", "<a style='color:palette(text)'")
48                        .arg(Constants::WEBSITE, Constants::APP_NAME)
49                        + "</h1>", this);
50     welcomeLabel->setOpenExternalLinks(true);
51     layout->addWidget(welcomeLabel);
52
53     layout->addSpacing(PADDING);
54
55     QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
56     tipLabel->setFont(biggerFont);
57     layout->addWidget(tipLabel);
58
59     layout->addSpacing(10);
60
61     QHBoxLayout *searchLayout = new QHBoxLayout();
62     searchLayout->setAlignment(Qt::AlignVCenter);
63
64     queryEdit = new SearchLineEdit(this);
65     queryEdit->setFont(biggerFont);
66     queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
67     queryEdit->sizeHint();
68     queryEdit->setFocus(Qt::OtherFocusReason);
69     connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
70     connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
71     searchLayout->addWidget(queryEdit);
72
73     searchLayout->addSpacing(10);
74
75     watchButton = new QPushButton(tr("Watch"), this);
76     watchButton->setDefault(true);
77     watchButton->setEnabled(false);
78     watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
79     connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
80     searchLayout->addWidget(watchButton);
81
82     layout->addItem(searchLayout);
83
84     layout->addSpacing(PADDING);
85
86     QHBoxLayout *otherLayout = new QHBoxLayout();
87
88     recentKeywordsLayout = new QVBoxLayout();
89     recentKeywordsLayout->setSpacing(5);
90     recentKeywordsLayout->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
91     recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
92     recentKeywordsLabel->hide();
93     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
94     recentKeywordsLabel->setFont(smallerFont);
95     recentKeywordsLayout->addWidget(recentKeywordsLabel);
96
97     otherLayout->addLayout(recentKeywordsLayout);
98
99     layout->addLayout(otherLayout);
100
101     mainLayout->addStretch();
102
103     setLayout(mainLayout);
104
105     updateChecker = 0;
106     checkForUpdate();
107 }
108
109 void SearchView::updateRecentKeywords() {
110
111     // cleanup
112     QLayoutItem *item;
113     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
114         item->widget()->close();
115         delete item;
116     }
117
118     // load
119     QSettings settings;
120     QStringList keywords = settings.value(recentKeywordsKey).toStringList();
121     recentKeywordsLabel->setVisible(!keywords.isEmpty());
122     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
123
124     foreach (QString keyword, keywords) {
125         QLabel *itemLabel = new QLabel("<a href=\"" + keyword
126                                        + "\" style=\"color:palette(text); text-decoration:none\">"
127                                        + keyword + "</a>", this);
128
129         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
130         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
131         // Make links navigable with the keyboard too
132         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
133
134         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
135         recentKeywordsLayout->addWidget(itemLabel);
136     }
137
138 }
139
140
141
142 void SearchView::watch() {
143     QString query = queryEdit->text().simplified();
144     watch(query);
145 }
146
147 void SearchView::textChanged(const QString &text) {
148     watchButton->setEnabled(!text.simplified().isEmpty());
149 }
150
151 void SearchView::watch(QString query) {
152
153     // check for empty query
154     if (query.length() == 0) {
155         queryEdit->setFocus(Qt::OtherFocusReason);
156         return;
157     }
158
159     // go!
160     emit search(query);
161 }
162
163 void SearchView::checkForUpdate() {
164     static const QString updateCheckKey = "updateCheck";
165
166     // check every 24h
167     QSettings settings;
168     uint unixTime = QDateTime::currentDateTime().toTime_t();
169     int lastCheck = settings.value(updateCheckKey).toInt();
170     int secondsSinceLastCheck = unixTime - lastCheck;
171     // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
172     if (secondsSinceLastCheck < 86400) return;
173
174     // check it out
175     if (updateChecker) delete updateChecker;
176     updateChecker = new UpdateChecker();
177     connect(updateChecker, SIGNAL(newVersion(QString)),
178             this, SLOT(gotNewVersion(QString)));
179     updateChecker->checkForUpdate();
180     settings.setValue(updateCheckKey, unixTime);
181
182 }
183
184 void SearchView::gotNewVersion(QString version) {
185     message->setText(
186             tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
187             .arg(
188                     Constants::APP_NAME,
189                     QString(Constants::WEBSITE).append("#download"),
190                     version)
191             );
192     message->setOpenExternalLinks(true);
193     message->setMargin(10);
194     message->setBackgroundRole(QPalette::ToolTipBase);
195     message->setForegroundRole(QPalette::ToolTipText);
196     message->setAutoFillBackground(true);
197     message->show();
198     if (updateChecker) delete updateChecker;
199 }