]> git.sur5r.net Git - minitube/blobdiff - src/SearchView.cpp
Imported Upstream version 1.4.1
[minitube] / src / SearchView.cpp
index d1907f0d68215f2d4f876273f6b664f0586b304e..809537ee41573773e06b4c8143e206b1036e901c 100644 (file)
@@ -1,12 +1,16 @@
 #include "SearchView.h"
 #include "constants.h"
 #include "fontutils.h"
+#include "searchparams.h"
+#include "youtubesuggest.h"
+#include "channelsuggest.h"
 
 namespace The {
     QMap<QString, QAction*>* globalActions();
 }
 
 static const QString recentKeywordsKey = "recentKeywords";
+static const QString recentChannelsKey = "recentChannels";
 static const int PADDING = 30;
 
 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
@@ -14,7 +18,7 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     QFont biggerFont = FontUtils::big();
     QFont smallerFont = FontUtils::smallBold();
 
-#ifdef APP_MAC
+#if defined(APP_MAC) | defined(APP_WIN)
     // speedup painting since we'll paint the whole background
     // by ourselves anyway in paintEvent()
     setAttribute(Qt::WA_OpaquePaintEvent);
@@ -29,6 +33,26 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     message->hide();
     mainLayout->addWidget(message);
 
+#ifdef APP_DEMO
+    QLabel *buy = new QLabel(this);
+    buy->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
+    buy->setText(QString("<a style='color:palette(text);text-decoration:none' href='%1'>%2</a>").arg(
+            QString(Constants::WEBSITE) + "#download",
+            tr("Get the full version").toUpper()
+            ));
+    buy->setOpenExternalLinks(true);
+    buy->setMargin(7);
+    buy->setAlignment(Qt::AlignRight);
+    buy->setStyleSheet("QLabel {"
+                       "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #CCD6E0, stop: 1 #ADBCCC);"
+                       "border-bottom-left-radius: 8px;"
+                       "border-bottom-right-radius: 8px;"
+                       "font-size: 10px;"
+                       "margin-right: 50px;"
+                       "}");
+    mainLayout->addWidget(buy, 0, Qt::AlignRight);
+#endif
+
     mainLayout->addStretch();
     mainLayout->addSpacing(PADDING);
 
@@ -57,9 +81,25 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 
     layout->addSpacing(PADDING / 2);
 
-    QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
+    QBoxLayout *tipLayout = new QHBoxLayout();
+    tipLayout->setSpacing(10);
+
+    //: "Enter", as in "type". The whole frase says: "Enter a keyword to start watching videos"
+    QLabel *tipLabel = new QLabel(tr("Enter"), this);
     tipLabel->setFont(biggerFont);
-    layout->addWidget(tipLabel);
+    tipLayout->addWidget(tipLabel);
+
+    typeCombo = new QComboBox(this);
+    typeCombo->addItem(tr("a keyword"));
+    typeCombo->addItem(tr("a channel"));
+    typeCombo->setFont(biggerFont);
+    connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
+    tipLayout->addWidget(typeCombo);
+
+    tipLabel = new QLabel(tr("to start watching videos."), this);
+    tipLabel->setFont(biggerFont);
+    tipLayout->addWidget(tipLabel);
+    layout->addLayout(tipLayout);
 
     layout->addSpacing(PADDING / 2);
 
@@ -73,8 +113,12 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     queryEdit->setFocus(Qt::OtherFocusReason);
     connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
     connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
-    searchLayout->addWidget(queryEdit);
 
+    youtubeSuggest = new YouTubeSuggest(this);
+    channelSuggest = new ChannelSuggest(this);
+    searchTypeChanged(0);
+
+    searchLayout->addWidget(queryEdit);
     searchLayout->addSpacing(10);
 
     watchButton = new QPushButton(tr("Watch"), this);
@@ -93,9 +137,9 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 
     recentKeywordsLayout = new QVBoxLayout();
     recentKeywordsLayout->setSpacing(5);
-    recentKeywordsLayout->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
+    recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
     recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
-#ifdef APP_MAC
+#if defined(APP_MAC) | defined(APP_WIN)
     QPalette palette = recentKeywordsLabel->palette();
     palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
     recentKeywordsLabel->setPalette(palette);
@@ -108,6 +152,24 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 
     otherLayout->addLayout(recentKeywordsLayout);
 
+    // recent channels
+    recentChannelsLayout = new QVBoxLayout();
+    recentChannelsLayout->setSpacing(5);
+    recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
+    recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
+#if defined(APP_MAC) | defined(APP_WIN)
+    palette = recentChannelsLabel->palette();
+    palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
+    recentChannelsLabel->setPalette(palette);
+#else
+    recentChannelsLabel->setForegroundRole(QPalette::Dark);
+#endif
+    recentChannelsLabel->hide();
+    recentChannelsLabel->setFont(smallerFont);
+    recentChannelsLayout->addWidget(recentChannelsLabel);
+
+    otherLayout->addLayout(recentChannelsLayout);
+
     layout->addLayout(otherLayout);
 
     mainLayout->addSpacing(PADDING);
@@ -116,7 +178,11 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
     setLayout(mainLayout);
 
     updateChecker = 0;
+
+#ifndef APP_MAC_STORE
     checkForUpdate();
+#endif
+
 }
 
 void SearchView::updateRecentKeywords() {
@@ -135,25 +201,72 @@ void SearchView::updateRecentKeywords() {
     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
 
     foreach (QString keyword, keywords) {
-        QLabel *itemLabel = new QLabel("<a href=\"" + keyword
+        QString link = keyword;
+        QString display = keyword;
+        if (keyword.startsWith("http://")) {
+            int separator = keyword.indexOf("|");
+            if (separator > 0 && separator + 1 < keyword.length()) {
+                link = keyword.left(separator);
+                display = keyword.mid(separator+1);
+            }
+        }
+        QLabel *itemLabel = new QLabel("<a href=\"" + link
                                        + "\" style=\"color:palette(text); text-decoration:none\">"
-                                       + keyword + "</a>", this);
+                                       + display + "</a>", this);
 
         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
         // Make links navigable with the keyboard too
         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
 
-        connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
+        connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
         recentKeywordsLayout->addWidget(itemLabel);
     }
 
 }
 
+void SearchView::updateRecentChannels() {
+
+    // cleanup
+    QLayoutItem *item;
+    while ((item = recentChannelsLayout->takeAt(1)) != 0) {
+        item->widget()->close();
+        delete item;
+    }
+
+    // load
+    QSettings settings;
+    QStringList keywords = settings.value(recentChannelsKey).toStringList();
+    recentChannelsLabel->setVisible(!keywords.isEmpty());
+    // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
+
+    foreach (QString keyword, keywords) {
+        QString link = keyword;
+        QString display = keyword;
+        if (keyword.startsWith("http://")) {
+            int separator = keyword.indexOf("|");
+            if (separator > 0 && separator + 1 < keyword.length()) {
+                link = keyword.left(separator);
+                display = keyword.mid(separator+1);
+            }
+        }
+        QLabel *itemLabel = new QLabel("<a href=\"" + link
+                                       + "\" style=\"color:palette(text); text-decoration:none\">"
+                                       + display + "</a>", this);
+
+        itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
+        // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+        // Make links navigable with the keyboard too
+        itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
+
+        connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
+        recentChannelsLayout->addWidget(itemLabel);
+    }
 
+}
 
 void SearchView::watch() {
-    QString query = queryEdit->text().simplified();
+    QString query = queryEdit->text();
     watch(query);
 }
 
@@ -163,14 +276,64 @@ void SearchView::textChanged(const QString &text) {
 
 void SearchView::watch(QString query) {
 
+    query = query.simplified();
+
+    // check for empty query
+    if (query.length() == 0) {
+        queryEdit->setFocus(Qt::OtherFocusReason);
+        return;
+    }
+
+    SearchParams *searchParams = new SearchParams();
+    if (typeCombo->currentIndex() == 0)
+        searchParams->setKeywords(query);
+    else {
+        // remove spaces from channel name
+        query = query.replace(" ", "");
+        searchParams->setAuthor(query);
+        searchParams->setSortBy(SearchParams::SortByNewest);
+    }
+
+    // go!
+    emit search(searchParams);
+}
+
+void SearchView::watchChannel(QString channel) {
+
+    channel = channel.simplified();
+
+    // check for empty query
+    if (channel.length() == 0) {
+        queryEdit->setFocus(Qt::OtherFocusReason);
+        return;
+    }
+
+    // remove spaces from channel name
+    channel = channel.replace(" ", "");
+
+    SearchParams *searchParams = new SearchParams();
+    searchParams->setAuthor(channel);
+    searchParams->setSortBy(SearchParams::SortByNewest);
+
+    // go!
+    emit search(searchParams);
+}
+
+void SearchView::watchKeywords(QString query) {
+
+    query = query.simplified();
+
     // check for empty query
     if (query.length() == 0) {
         queryEdit->setFocus(Qt::OtherFocusReason);
         return;
     }
 
+    SearchParams *searchParams = new SearchParams();
+    searchParams->setKeywords(query);
+
     // go!
-    emit search(query);
+    emit search(searchParams);
 }
 
 void SearchView::checkForUpdate() {
@@ -215,7 +378,7 @@ void SearchView::gotNewVersion(QString version) {
 }
 
 void SearchView::paintEvent(QPaintEvent * /*event*/) {
-#ifdef APP_MAC
+#if defined(APP_MAC) | defined(APP_WIN)
     QBrush brush;
     if (window()->isActiveWindow()) {
         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
@@ -226,3 +389,12 @@ void SearchView::paintEvent(QPaintEvent * /*event*/) {
     painter.fillRect(0, 0, width(), height(), brush);
 #endif
 }
+
+void SearchView::searchTypeChanged(int index) {
+    if (index == 0) {
+        queryEdit->setSuggester(youtubeSuggest);
+    } else {
+        queryEdit->setSuggester(channelSuggest);
+    }
+    queryEdit->setFocus();
+}