]> git.sur5r.net Git - minitube/blob - src/SearchView.cpp
Imported Upstream version 1.4.1
[minitube] / src / SearchView.cpp
1 #include "SearchView.h"
2 #include "constants.h"
3 #include "fontutils.h"
4 #include "searchparams.h"
5 #include "youtubesuggest.h"
6 #include "channelsuggest.h"
7
8 namespace The {
9     QMap<QString, QAction*>* globalActions();
10 }
11
12 static const QString recentKeywordsKey = "recentKeywords";
13 static const QString recentChannelsKey = "recentChannels";
14 static const int PADDING = 30;
15
16 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
17
18     QFont biggerFont = FontUtils::big();
19     QFont smallerFont = FontUtils::smallBold();
20
21 #if defined(APP_MAC) | defined(APP_WIN)
22     // speedup painting since we'll paint the whole background
23     // by ourselves anyway in paintEvent()
24     setAttribute(Qt::WA_OpaquePaintEvent);
25 #endif
26
27     QBoxLayout *mainLayout = new QVBoxLayout();
28     mainLayout->setMargin(0);
29     mainLayout->setSpacing(0);
30
31     // hidden message widget
32     message = new QLabel(this);
33     message->hide();
34     mainLayout->addWidget(message);
35
36 #ifdef APP_DEMO
37     QLabel *buy = new QLabel(this);
38     buy->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
39     buy->setText(QString("<a style='color:palette(text);text-decoration:none' href='%1'>%2</a>").arg(
40             QString(Constants::WEBSITE) + "#download",
41             tr("Get the full version").toUpper()
42             ));
43     buy->setOpenExternalLinks(true);
44     buy->setMargin(7);
45     buy->setAlignment(Qt::AlignRight);
46     buy->setStyleSheet("QLabel {"
47                        "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #CCD6E0, stop: 1 #ADBCCC);"
48                        "border-bottom-left-radius: 8px;"
49                        "border-bottom-right-radius: 8px;"
50                        "font-size: 10px;"
51                        "margin-right: 50px;"
52                        "}");
53     mainLayout->addWidget(buy, 0, Qt::AlignRight);
54 #endif
55
56     mainLayout->addStretch();
57     mainLayout->addSpacing(PADDING);
58
59     QBoxLayout *hLayout = new QHBoxLayout();
60     hLayout->setAlignment(Qt::AlignCenter);
61     mainLayout->addLayout(hLayout);
62
63     QLabel *logo = new QLabel(this);
64     logo->setPixmap(QPixmap(":/images/app.png"));
65     hLayout->addWidget(logo, 0, Qt::AlignTop);
66     hLayout->addSpacing(PADDING);
67
68     QVBoxLayout *layout = new QVBoxLayout();
69     layout->setAlignment(Qt::AlignCenter);
70     hLayout->addLayout(layout);
71
72     QLabel *welcomeLabel =
73             new QLabel("<h1 style='font-weight:normal'>" +
74                        tr("Welcome to <a href='%1'>%2</a>,")
75                        // .replace("<a ", "<a style='color:palette(text)'")
76                        .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
77                        .arg(Constants::WEBSITE, Constants::APP_NAME)
78                        + "</h1>", this);
79     welcomeLabel->setOpenExternalLinks(true);
80     layout->addWidget(welcomeLabel);
81
82     layout->addSpacing(PADDING / 2);
83
84     QBoxLayout *tipLayout = new QHBoxLayout();
85     tipLayout->setSpacing(10);
86
87     //: "Enter", as in "type". The whole frase says: "Enter a keyword to start watching videos"
88     QLabel *tipLabel = new QLabel(tr("Enter"), this);
89     tipLabel->setFont(biggerFont);
90     tipLayout->addWidget(tipLabel);
91
92     typeCombo = new QComboBox(this);
93     typeCombo->addItem(tr("a keyword"));
94     typeCombo->addItem(tr("a channel"));
95     typeCombo->setFont(biggerFont);
96     connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
97     tipLayout->addWidget(typeCombo);
98
99     tipLabel = new QLabel(tr("to start watching videos."), this);
100     tipLabel->setFont(biggerFont);
101     tipLayout->addWidget(tipLabel);
102     layout->addLayout(tipLayout);
103
104     layout->addSpacing(PADDING / 2);
105
106     QHBoxLayout *searchLayout = new QHBoxLayout();
107     searchLayout->setAlignment(Qt::AlignVCenter);
108
109     queryEdit = new SearchLineEdit(this);
110     queryEdit->setFont(biggerFont);
111     queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
112     queryEdit->sizeHint();
113     queryEdit->setFocus(Qt::OtherFocusReason);
114     connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
115     connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
116
117     youtubeSuggest = new YouTubeSuggest(this);
118     channelSuggest = new ChannelSuggest(this);
119     searchTypeChanged(0);
120
121     searchLayout->addWidget(queryEdit);
122     searchLayout->addSpacing(10);
123
124     watchButton = new QPushButton(tr("Watch"), this);
125     watchButton->setDefault(true);
126     watchButton->setEnabled(false);
127     watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
128     connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
129     searchLayout->addWidget(watchButton);
130
131     layout->addItem(searchLayout);
132
133     layout->addSpacing(PADDING / 2);
134
135     QHBoxLayout *otherLayout = new QHBoxLayout();
136     otherLayout->setMargin(0);
137
138     recentKeywordsLayout = new QVBoxLayout();
139     recentKeywordsLayout->setSpacing(5);
140     recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
141     recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
142 #if defined(APP_MAC) | defined(APP_WIN)
143     QPalette palette = recentKeywordsLabel->palette();
144     palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
145     recentKeywordsLabel->setPalette(palette);
146 #else
147     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
148 #endif
149     recentKeywordsLabel->hide();
150     recentKeywordsLabel->setFont(smallerFont);
151     recentKeywordsLayout->addWidget(recentKeywordsLabel);
152
153     otherLayout->addLayout(recentKeywordsLayout);
154
155     // recent channels
156     recentChannelsLayout = new QVBoxLayout();
157     recentChannelsLayout->setSpacing(5);
158     recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
159     recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
160 #if defined(APP_MAC) | defined(APP_WIN)
161     palette = recentChannelsLabel->palette();
162     palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
163     recentChannelsLabel->setPalette(palette);
164 #else
165     recentChannelsLabel->setForegroundRole(QPalette::Dark);
166 #endif
167     recentChannelsLabel->hide();
168     recentChannelsLabel->setFont(smallerFont);
169     recentChannelsLayout->addWidget(recentChannelsLabel);
170
171     otherLayout->addLayout(recentChannelsLayout);
172
173     layout->addLayout(otherLayout);
174
175     mainLayout->addSpacing(PADDING);
176     mainLayout->addStretch();
177
178     setLayout(mainLayout);
179
180     updateChecker = 0;
181
182 #ifndef APP_MAC_STORE
183     checkForUpdate();
184 #endif
185
186 }
187
188 void SearchView::updateRecentKeywords() {
189
190     // cleanup
191     QLayoutItem *item;
192     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
193         item->widget()->close();
194         delete item;
195     }
196
197     // load
198     QSettings settings;
199     QStringList keywords = settings.value(recentKeywordsKey).toStringList();
200     recentKeywordsLabel->setVisible(!keywords.isEmpty());
201     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
202
203     foreach (QString keyword, keywords) {
204         QString link = keyword;
205         QString display = keyword;
206         if (keyword.startsWith("http://")) {
207             int separator = keyword.indexOf("|");
208             if (separator > 0 && separator + 1 < keyword.length()) {
209                 link = keyword.left(separator);
210                 display = keyword.mid(separator+1);
211             }
212         }
213         QLabel *itemLabel = new QLabel("<a href=\"" + link
214                                        + "\" style=\"color:palette(text); text-decoration:none\">"
215                                        + display + "</a>", this);
216
217         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
218         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
219         // Make links navigable with the keyboard too
220         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
221
222         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
223         recentKeywordsLayout->addWidget(itemLabel);
224     }
225
226 }
227
228 void SearchView::updateRecentChannels() {
229
230     // cleanup
231     QLayoutItem *item;
232     while ((item = recentChannelsLayout->takeAt(1)) != 0) {
233         item->widget()->close();
234         delete item;
235     }
236
237     // load
238     QSettings settings;
239     QStringList keywords = settings.value(recentChannelsKey).toStringList();
240     recentChannelsLabel->setVisible(!keywords.isEmpty());
241     // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
242
243     foreach (QString keyword, keywords) {
244         QString link = keyword;
245         QString display = keyword;
246         if (keyword.startsWith("http://")) {
247             int separator = keyword.indexOf("|");
248             if (separator > 0 && separator + 1 < keyword.length()) {
249                 link = keyword.left(separator);
250                 display = keyword.mid(separator+1);
251             }
252         }
253         QLabel *itemLabel = new QLabel("<a href=\"" + link
254                                        + "\" style=\"color:palette(text); text-decoration:none\">"
255                                        + display + "</a>", this);
256
257         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
258         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
259         // Make links navigable with the keyboard too
260         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
261
262         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
263         recentChannelsLayout->addWidget(itemLabel);
264     }
265
266 }
267
268 void SearchView::watch() {
269     QString query = queryEdit->text();
270     watch(query);
271 }
272
273 void SearchView::textChanged(const QString &text) {
274     watchButton->setEnabled(!text.simplified().isEmpty());
275 }
276
277 void SearchView::watch(QString query) {
278
279     query = query.simplified();
280
281     // check for empty query
282     if (query.length() == 0) {
283         queryEdit->setFocus(Qt::OtherFocusReason);
284         return;
285     }
286
287     SearchParams *searchParams = new SearchParams();
288     if (typeCombo->currentIndex() == 0)
289         searchParams->setKeywords(query);
290     else {
291         // remove spaces from channel name
292         query = query.replace(" ", "");
293         searchParams->setAuthor(query);
294         searchParams->setSortBy(SearchParams::SortByNewest);
295     }
296
297     // go!
298     emit search(searchParams);
299 }
300
301 void SearchView::watchChannel(QString channel) {
302
303     channel = channel.simplified();
304
305     // check for empty query
306     if (channel.length() == 0) {
307         queryEdit->setFocus(Qt::OtherFocusReason);
308         return;
309     }
310
311     // remove spaces from channel name
312     channel = channel.replace(" ", "");
313
314     SearchParams *searchParams = new SearchParams();
315     searchParams->setAuthor(channel);
316     searchParams->setSortBy(SearchParams::SortByNewest);
317
318     // go!
319     emit search(searchParams);
320 }
321
322 void SearchView::watchKeywords(QString query) {
323
324     query = query.simplified();
325
326     // check for empty query
327     if (query.length() == 0) {
328         queryEdit->setFocus(Qt::OtherFocusReason);
329         return;
330     }
331
332     SearchParams *searchParams = new SearchParams();
333     searchParams->setKeywords(query);
334
335     // go!
336     emit search(searchParams);
337 }
338
339 void SearchView::checkForUpdate() {
340     static const QString updateCheckKey = "updateCheck";
341
342     // check every 24h
343     QSettings settings;
344     uint unixTime = QDateTime::currentDateTime().toTime_t();
345     int lastCheck = settings.value(updateCheckKey).toInt();
346     int secondsSinceLastCheck = unixTime - lastCheck;
347     // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
348     if (secondsSinceLastCheck < 86400) return;
349
350     // check it out
351     if (updateChecker) delete updateChecker;
352     updateChecker = new UpdateChecker();
353     connect(updateChecker, SIGNAL(newVersion(QString)),
354             this, SLOT(gotNewVersion(QString)));
355     updateChecker->checkForUpdate();
356     settings.setValue(updateCheckKey, unixTime);
357
358 }
359
360 void SearchView::gotNewVersion(QString version) {
361     message->setText(
362             tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
363             .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
364             .arg(
365                     Constants::APP_NAME,
366                     QString(Constants::WEBSITE).append("#download"),
367                     version)
368             );
369     message->setOpenExternalLinks(true);
370     message->setMargin(10);
371     message->setAlignment(Qt::AlignCenter);
372     // message->setBackgroundRole(QPalette::ToolTipBase);
373     // message->setForegroundRole(QPalette::ToolTipText);
374     // message->setAutoFillBackground(true);
375     message->setStyleSheet("QLabel { border-bottom: 1px solid palette(mid); }");
376     message->show();
377     if (updateChecker) delete updateChecker;
378 }
379
380 void SearchView::paintEvent(QPaintEvent * /*event*/) {
381 #if defined(APP_MAC) | defined(APP_WIN)
382     QBrush brush;
383     if (window()->isActiveWindow()) {
384         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
385     } else {
386         brush = palette().window();
387     }
388     QPainter painter(this);
389     painter.fillRect(0, 0, width(), height(), brush);
390 #endif
391 }
392
393 void SearchView::searchTypeChanged(int index) {
394     if (index == 0) {
395         queryEdit->setSuggester(youtubeSuggest);
396     } else {
397         queryEdit->setSuggester(channelSuggest);
398     }
399     queryEdit->setFocus();
400 }