]> git.sur5r.net Git - minitube/blobdiff - src/ListModel.cpp
Merge commit 'refs/merge-requests/4' of git://gitorious.org/minitube/minitube
[minitube] / src / ListModel.cpp
index e794d3de3ddf68ef35419f45e27221669ac7fbf0..761e5a4bff6c200c8ed49dba0bfdff35751b4408 100755 (executable)
@@ -2,6 +2,7 @@
 #include "videomimedata.h"
 
 #define MAX_ITEMS 10
+static const QString recentKeywordsKey = "recentKeywords";
 
 ListModel::ListModel(QWidget *parent) : QAbstractListModel(parent) {
     youtubeSearch = 0;
@@ -41,6 +42,7 @@ QVariant ListModel::data(const QModelIndex &index, int role) const {
             return ItemTypeShowMore;
         case Qt::DisplayRole:
         case Qt::StatusTipRole:
+            if (!errorMessage.isEmpty()) return errorMessage;
             if (searching) return tr("Searching...");
             if (canSearchMore) return tr("Show %1 More").arg(MAX_ITEMS);
             if (videos.isEmpty()) return tr("No videos");
@@ -48,7 +50,15 @@ QVariant ListModel::data(const QModelIndex &index, int role) const {
         case Qt::TextAlignmentRole:
             return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
         case Qt::ForegroundRole:
-            return palette.color(QPalette::Dark);
+            if (!errorMessage.isEmpty())
+                return palette.color(QPalette::ToolTipText);
+            else
+                return palette.color(QPalette::Dark);
+        case Qt::BackgroundColorRole:
+            if (!errorMessage.isEmpty())
+                return palette.color(QPalette::ToolTipBase);
+            else
+                return QVariant();
         case Qt::FontRole:
             return boldFont;
         default:
@@ -68,29 +78,7 @@ QVariant ListModel::data(const QModelIndex &index, int role) const {
     case ActiveTrackRole:
         return video == m_activeVideo;
     case Qt::DisplayRole:
-    case Qt::StatusTipRole:
         return video->title();
-        /*
-        case Qt::ToolTipRole:
-          
-            QString tooltip;
-            if (!element.firstChildElement().text().isEmpty()) {
-                tooltip.append(QString("<b>").append(element.firstChildElement().text()).append("</b><br/>"));
-            }
-            if (!fromDate.isEmpty()) {
-                tooltip.append("<i>Pubblicato il</i> ").append(fromDate);
-            }
-            if (!toDate.isEmpty()) {
-                tooltip.append("<br/><i>Scadenza</i>: ").append(toDate);
-            }
-            tooltip.append("<br/><i>Tipo</i>: ").append(typeName)
-                .append("<br/><i>Id</i>: ").appen    QFont boldFont;
-    boldFont.setBold(true);d(id);
-            return tooltip;
-            */
-        
-        // case StreamUrlRole:
-        // return video->streamUrl();
     }
     
     return QVariant();
@@ -102,8 +90,6 @@ void ListModel::setActiveRow( int row) {
         m_activeRow = row;
         m_activeVideo = videoAt(row);
         
-        // setStateOfRow( row, Item::Played );
-        
         int oldactiverow = m_activeRow;
         
         if ( rowExists( oldactiverow ) )
@@ -144,6 +130,7 @@ void ListModel::search(SearchParams *searchParams) {
     m_activeVideo = 0;
     m_activeRow = -1;
     skip = 1;
+    errorMessage.clear();
     reset();
 
     // (re)initialize the YouTubeSearch
@@ -151,6 +138,7 @@ void ListModel::search(SearchParams *searchParams) {
     youtubeSearch = new YouTubeSearch();
     connect(youtubeSearch, SIGNAL(gotVideo(Video*)), this, SLOT(addVideo(Video*)));
     connect(youtubeSearch, SIGNAL(finished(int)), this, SLOT(searchFinished(int)));
+    connect(youtubeSearch, SIGNAL(error(QString)), this, SLOT(searchError(QString)));
 
     this->searchParams = searchParams;
     searching = true;
@@ -161,6 +149,7 @@ void ListModel::search(SearchParams *searchParams) {
 void ListModel::searchMore(int max) {
     if (searching) return;
     searching = true;
+    errorMessage.clear();
     youtubeSearch->search(searchParams, max, skip);
     skip += max;
 }
@@ -187,6 +176,15 @@ void ListModel::abortSearch() {
 void ListModel::searchFinished(int total) {
     searching = false;
     canSearchMore = total > 0;
+
+    // update the message item
+    emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
+}
+
+void ListModel::searchError(QString message) {
+    errorMessage = message;
+    // update the message item
+    emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
 }
 
 void ListModel::addVideo(Video* video) {
@@ -197,9 +195,20 @@ void ListModel::addVideo(Video* video) {
     videos << video;
     endInsertRows();
     
-    // autoplay
+    // first result!
     if (videos.size() == 1) {
+        // autoplay
         setActiveRow(0);
+
+        // save keyword
+        QString query = searchParams->keywords();
+        QSettings settings;
+        QStringList keywords = settings.value(recentKeywordsKey).toStringList();
+        keywords.removeAll(query);
+        keywords.prepend(query);
+        while (keywords.size() > 10)
+            keywords.removeLast();
+        settings.setValue(recentKeywordsKey, keywords);
     }
 
 }
@@ -235,6 +244,7 @@ void ListModel::removeIndexes(QModelIndexList &indexes) {
     QList<Video*> originalList(videos);
     QList<Video*> delitems;
     foreach (QModelIndex index, indexes) {
+        if (index.row() >= originalList.size()) continue;
         Video* video = originalList.at(index.row());
         int idx = videos.indexOf(video);
         if (idx != -1) {
@@ -344,21 +354,22 @@ QModelIndex ListModel::indexForVideo(Video* video) {
 }
 
 void ListModel::move(QModelIndexList &indexes, bool up) {
-
     QList<Video*> movedVideos;
 
     foreach (QModelIndex index, indexes) {
         int row = index.row();
-        qDebug() << "index row" << row;
+        if (row >= videos.size()) continue;
+        // qDebug() << "index row" << row;
         Video *video = videoAt(row);
         movedVideos << video;
     }
 
-    int counter = 1;
+    int end=up ? -1 : rowCount()-1, mod=up ? -1 : 1;
     foreach (Video *video, movedVideos) {
 
         int row = rowForVideo(video);
-        qDebug() << "video row" << row;
+        if (row+mod==end) { end=row; continue; }
+        // qDebug() << "video row" << row;
         removeRows(row, 1, QModelIndex());
 
         if (up) row--;
@@ -368,7 +379,6 @@ void ListModel::move(QModelIndexList &indexes, bool up) {
         videos.insert(row, video);
         endInsertRows();
 
-        counter++;
     }
 
     emit needSelectionFor(movedVideos);