]> git.sur5r.net Git - minitube/commitdiff
Subscriptions context menu
authorFlavio <flavio@odisseo.local>
Fri, 18 Jul 2014 21:04:37 +0000 (23:04 +0200)
committerFlavio <flavio@odisseo.local>
Fri, 18 Jul 2014 21:04:37 +0000 (23:04 +0200)
src/channelaggregator.h
src/channelmodel.cpp
src/channelmodel.h
src/channelview.cpp
src/channelview.h
src/ytuser.cpp
src/ytuser.h

index 966fa704fd4d2aa48ebb4dae3d63ee57e9ddbfc9..1ef224f7644f900737880a244f02b5870002752d 100644 (file)
@@ -41,6 +41,7 @@ public slots:
     void start();
     void stop();
     void run();
+    void updateUnwatchedCount();
 
 signals:
     void channelChanged(YTUser*);
@@ -54,7 +55,6 @@ private:
     YTUser* getChannelToCheck();
     void processNextChannel();
     void addVideo(Video* video);
-    void updateUnwatchedCount();
     void finish();
 
     uint checkInterval;
index 2aa6c43d351454cd05af4a9739f0960e9f698c2b..fb8cbdb577736e3c516b7b1a4e2968381b3ce3c0 100644 (file)
@@ -56,6 +56,8 @@ QVariant ChannelModel::data(const QModelIndex &index, int role) const {
 }
 
 YTUser* ChannelModel::userForIndex(const QModelIndex &index) const {
+    const int row = index.row();
+    if (row < channelOffset) return 0;
     return channels.at(index.row() - channelOffset);
 }
 
@@ -84,6 +86,8 @@ void ChannelModel::setQuery(const QString &query, const QSqlDatabase &db) {
     while (q.next()) {
         YTUser *user = YTUser::forId(q.value(0).toString());
         connect(user, SIGNAL(thumbnailLoaded()), SLOT(updateSender()), Qt::UniqueConnection);
+        connect(user, SIGNAL(notifyCountChanged()), SLOT(updateSender()), Qt::UniqueConnection);
+        connect(user, SIGNAL(destroyed(QObject *)), SLOT(removeChannel(QObject *)), Qt::UniqueConnection);
         channels << user;
     }
 
@@ -116,6 +120,20 @@ void ChannelModel::updateUnwatched() {
     emit dataChanged(i, i);
 }
 
+void ChannelModel::removeChannel(QObject *obj) {
+    YTUser *user = static_cast<YTUser*>(obj);
+    qWarning() << "user is" << user << obj << obj->metaObject()->className();
+    if (!user) return;
+
+    int row = channels.indexOf(user);
+    if (row == -1) return;
+
+    int position = row + channelOffset;
+    beginRemoveRows(QModelIndex(), position, position+1);
+    channels.removeAt(row);
+    endRemoveRows();
+}
+
 void ChannelModel::setHoveredRow(int row) {
     int oldRow = hoveredRow;
     hoveredRow = row;
index 3d89d4dbc15cb8b400712bdd2be64d1833f80f80..6ec5cf1dc7841b42bd680d7c9693ace19d4cb1b6 100644 (file)
@@ -59,6 +59,7 @@ public slots:
     void updateSender();
     void updateChannel(YTUser *user);
     void updateUnwatched();
+    void removeChannel(QObject *obj);
 
 private:
     QList<YTUser*> channels;
index eba059135686298690a3ae9b96d6edaebc09330f..eef1a7200fbd7ac7e7122826551c1cdc4fa6e9a8 100644 (file)
@@ -72,6 +72,10 @@ ChannelView::ChannelView(QWidget *parent) : QListView(parent),
 
     setMouseTracking(true);
 
+    setContextMenuPolicy(Qt::CustomContextMenu);
+    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
+            SLOT(showContextMenu(const QPoint &)));
+
     connect(this, SIGNAL(clicked(const QModelIndex &)),
             SLOT(itemActivated(const QModelIndex &)));
     connect(this, SIGNAL(entered(const QModelIndex &)),
@@ -95,22 +99,6 @@ ChannelView::ChannelView(QWidget *parent) : QListView(parent),
 void ChannelView::setupActions() {
     QSettings settings;
 
-    markAsWatchedAction = new QAction(
-                Utils::icon("mark-watched"), tr("Mark all as watched"), this);
-    markAsWatchedAction->setEnabled(false);
-    markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W));
-    connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched()));
-    statusActions << markAsWatchedAction;
-
-    showUpdated = settings.value(showUpdatedKey, false).toBool();
-    QAction *showUpdatedAction = new QAction(
-                Utils::icon("show-updated"), tr("Show Updated"), this);
-    showUpdatedAction->setCheckable(true);
-    showUpdatedAction->setChecked(showUpdated);
-    showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U));
-    connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool)));
-    statusActions << showUpdatedAction;
-
     SortBy sortBy = static_cast<SortBy>(settings.value(sortByKey, SortByName).toInt());
 
     QMenu *sortMenu = new QMenu(this);
@@ -163,6 +151,22 @@ void ChannelView::setupActions() {
     widgetAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O));
     statusActions << widgetAction;
 
+    markAsWatchedAction = new QAction(
+                Utils::icon("mark-watched"), tr("Mark all as watched"), this);
+    markAsWatchedAction->setEnabled(false);
+    markAsWatchedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_W));
+    connect(markAsWatchedAction, SIGNAL(triggered()), SLOT(markAllAsWatched()));
+    statusActions << markAsWatchedAction;
+
+    showUpdated = settings.value(showUpdatedKey, false).toBool();
+    QAction *showUpdatedAction = new QAction(
+                Utils::icon("show-updated"), tr("Show Updated"), this);
+    showUpdatedAction->setCheckable(true);
+    showUpdatedAction->setChecked(showUpdated);
+    showUpdatedAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_U));
+    connect(showUpdatedAction, SIGNAL(toggled(bool)), SLOT(toggleShowUpdated(bool)));
+    statusActions << showUpdatedAction;
+
     foreach (QAction *action, statusActions) {
         window()->addAction(action);
         Utils::setupAction(action);
@@ -221,6 +225,37 @@ void ChannelView::itemActivated(const QModelIndex &index) {
     }
 }
 
+void ChannelView::showContextMenu(const QPoint &point) {
+    const QModelIndex index = indexAt(point);
+    if (!index.isValid()) return;
+
+    YTUser *user = channelsModel->userForIndex(index);
+    if (!user) return;
+
+    unsetCursor();
+
+    QMenu menu;
+
+    QAction *markAsWatchedAction = menu.addAction(tr("Mark as Watched"), user, SLOT(updateWatched()));
+    connect(markAsWatchedAction, SIGNAL(triggered()),
+            ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
+
+    menu.addSeparator();
+
+    /*
+    // TODO
+    QAction *notificationsAction = menu.addAction(tr("Receive Notifications"), user, SLOT(unsubscribe()));
+    notificationsAction->setCheckable(true);
+    notificationsAction->setChecked(true);
+    */
+
+    QAction *unsubscribeAction = menu.addAction(tr("Unsubscribe"), user, SLOT(unsubscribe()));
+    connect(unsubscribeAction, SIGNAL(triggered()),
+            ChannelAggregator::instance(), SLOT(updateUnwatchedCount()));
+
+    menu.exec(mapToGlobal(point));
+}
+
 void ChannelView::paintEvent(QPaintEvent *event) {
     if (model()->rowCount() < 3) {
         QString msg;
index a5013c9571b9bd35c49cb6a605df8e5c09eecd07..1a6fd5afd6bce94990147e5d202a99a8e3f03595 100644 (file)
@@ -58,6 +58,7 @@ private:
 private slots:
     void itemEntered(const QModelIndex &index);
     void itemActivated(const QModelIndex &index);
+    void showContextMenu(const QPoint &point);
     void toggleShowUpdated(bool enable);
     void setSortBy(SortBy sortBy);
     void setSortByName() { setSortBy(SortByName); }
@@ -67,9 +68,9 @@ private slots:
     void setSortByMostWatched() { setSortBy(SortByMostWatched); }
     void markAllAsWatched();
     void unwatchedCountChanged(int count);
+    void updateQuery(bool transition = false);
 
 private:
-    void updateQuery(bool transition = false);
     void setupActions();
 
     ChannelModel *channelsModel;
index 9dc633adcb8bac2531a17f6e2a4c257acf6298ba..5098f3f84d56196f5185f06e06447b882b828bdc 100644 (file)
@@ -142,6 +142,10 @@ QString YTUser::getThumbnailLocation() {
     return getThumbnailDir() + userId;
 }
 
+void YTUser::unsubscribe() {
+    YTUser::unsubscribe(userId);
+}
+
 void YTUser::storeThumbnail(QByteArray bytes) {
     thumbnail.loadFromData(bytes);
     static const int maxWidth = 88;
@@ -265,6 +269,7 @@ void YTUser::updateWatched() {
     uint now = QDateTime::currentDateTime().toTime_t();
     watched = now;
     notifyCount = 0;
+    emit notifyCountChanged();
 
     QSqlDatabase db = Database::instance().getConnection();
     QSqlQuery query(db);
@@ -276,6 +281,8 @@ void YTUser::updateWatched() {
 }
 
 void YTUser::storeNotifyCount(int count) {
+    if (notifyCount != count)
+        emit notifyCountChanged();
     notifyCount = count;
 
     QSqlDatabase db = Database::instance().getConnection();
index d7131cdc9ca9b71c94bc3ef042e2fe8e2f29b1d3..631a36601ff217eef25dc64d1cfb4f4a349d06e9 100644 (file)
@@ -42,7 +42,6 @@ public:
 
     uint getWatched() const { return watched; }
     void setWatched(uint watched) { this->watched = watched; }
-    void updateWatched();
 
     int getNotifyCount() const { return notifyCount; }
     void setNotifyCount(int count) { notifyCount = count; }
@@ -62,10 +61,15 @@ public:
 
     static QList<YTUser*> getCachedUsers() { return cache.values(); }
 
+public slots:
+    void updateWatched();
+    void unsubscribe();
+
 signals:
     void infoLoaded();
     void thumbnailLoaded();
     void error(QString message);
+    void notifyCountChanged();
 
 private slots:
     void parseResponse(QByteArray bytes);