]> git.sur5r.net Git - minitube/blobdiff - src/SearchView.cpp
Imported Upstream version 1.6
[minitube] / src / SearchView.cpp
index 76f7da4abe9980b65789db1f3b7c1b6bda2e717f..4bfd3fda944c59b703e67d3f5e0f104e860c73a0 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);
 
@@ -50,16 +74,32 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
                        tr("Welcome to <a href='%1'>%2</a>,")
                        // .replace("<a ", "<a style='color:palette(text)'")
                        .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
-                       .arg(Constants::WEBSITE, Constants::APP_NAME)
+                       .arg(Constants::WEBSITE, Constants::NAME)
                        + "</h1>", this);
     welcomeLabel->setOpenExternalLinks(true);
     layout->addWidget(welcomeLabel);
 
     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);
@@ -90,12 +134,13 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 
     QHBoxLayout *otherLayout = new QHBoxLayout();
     otherLayout->setMargin(0);
+    otherLayout->setSpacing(10);
 
     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 +153,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);
@@ -115,8 +178,6 @@ SearchView::SearchView(QWidget *parent) : QWidget(parent) {
 
     setLayout(mainLayout);
 
-    updateChecker = 0;
-    checkForUpdate();
 }
 
 void SearchView::updateRecentKeywords() {
@@ -137,32 +198,77 @@ void SearchView::updateRecentKeywords() {
     foreach (QString keyword, keywords) {
         QString link = keyword;
         QString display = keyword;
-        if (keyword.startsWith("http://")) {
+        if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
             int separator = keyword.indexOf("|");
             if (separator > 0 && separator + 1 < keyword.length()) {
                 link = keyword.left(separator);
                 display = keyword.mid(separator+1);
             }
         }
+        bool needStatusTip = false;
+        if (display.length() > 24) {
+            display.truncate(24);
+            display.append("...");
+            needStatusTip = true;
+        }
         QLabel *itemLabel = new QLabel("<a href=\"" + link
                                        + "\" style=\"color:palette(text); text-decoration:none\">"
                                        + display + "</a>", this);
-
+        itemLabel->setAttribute(Qt::WA_DeleteOnClose);
         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)));
+        if (needStatusTip)
+            itemLabel->setStatusTip(link);
+        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://") || keyword.startsWith("https://")) {
+            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->setAttribute(Qt::WA_DeleteOnClose);
+        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);
 }
 
@@ -172,59 +278,68 @@ 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(query);
+    emit search(searchParams);
 }
 
-void SearchView::checkForUpdate() {
-    static const QString updateCheckKey = "updateCheck";
+void SearchView::watchChannel(QString channel) {
 
-    // check every 24h
-    QSettings settings;
-    uint unixTime = QDateTime::currentDateTime().toTime_t();
-    int lastCheck = settings.value(updateCheckKey).toInt();
-    int secondsSinceLastCheck = unixTime - lastCheck;
-    // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
-    if (secondsSinceLastCheck < 86400) return;
-
-    // check it out
-    if (updateChecker) delete updateChecker;
-    updateChecker = new UpdateChecker();
-    connect(updateChecker, SIGNAL(newVersion(QString)),
-            this, SLOT(gotNewVersion(QString)));
-    updateChecker->checkForUpdate();
-    settings.setValue(updateCheckKey, unixTime);
+    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::gotNewVersion(QString version) {
-    message->setText(
-            tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
-            .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
-            .arg(
-                    Constants::APP_NAME,
-                    QString(Constants::WEBSITE).append("#download"),
-                    version)
-            );
-    message->setOpenExternalLinks(true);
-    message->setMargin(10);
-    message->setAlignment(Qt::AlignCenter);
-    // message->setBackgroundRole(QPalette::ToolTipBase);
-    // message->setForegroundRole(QPalette::ToolTipText);
-    // message->setAutoFillBackground(true);
-    message->setStyleSheet("QLabel { border-bottom: 1px solid palette(mid); }");
-    message->show();
-    if (updateChecker) delete updateChecker;
+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(searchParams);
 }
 
 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));
@@ -235,3 +350,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();
+}