rssmanager.cpp 5.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Bittorrent Client using Qt4 and libtorrent.
 * Copyright (C) 2010  Christophe Dumez, Arnaud Demaiziere
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link this program with the OpenSSL project's "OpenSSL" library (or with
 * modified versions of it that use the same license as the "OpenSSL" library),
 * and distribute the linked executables. You must obey the GNU General Public
 * License in all respects for all of the code used other than "OpenSSL".  If you
 * modify file(s), you may extend this exception to your version of the file(s),
 * but you are not obligated to do so. If you do not wish to do so, delete this
 * exception statement from your version.
 *
 * Contact: chris@qbittorrent.org, arnaud@qbittorrent.org
 */

31
#include <QDebug>
32 33 34 35 36 37
#include "rssmanager.h"
#include "rsssettings.h"
#include "qbtsession.h"
#include "rssfeed.h"
#include "rssarticle.h"
#include "rssdownloadrulelist.h"
38
#include "downloadthread.h"
39 40 41 42

RssManager* RssManager::m_instance = 0;

RssManager::RssManager(): RssFolder() {
43
  m_rssDownloader = new DownloadThread(this);
44 45 46
  connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
  m_refreshInterval = RssSettings().getRSSRefreshInterval();
  m_refreshTimer.start(m_refreshInterval*60000);
47 48 49 50
}

RssManager::~RssManager(){
  qDebug("Deleting RSSManager");
51
  m_refreshTimer.stop();
52
  delete m_rssDownloader;
53
  RssDownloadRuleList::drop();
54
  saveItemsToDisk();
55 56 57 58
  saveStreamList();
  qDebug("RSSManager deleted");
}

59 60 61 62 63
void RssManager::updateRefreshInterval(uint val){
  if(m_refreshInterval != val) {
    m_refreshInterval = val;
    m_refreshTimer.start(m_refreshInterval*60000);
    qDebug("New RSS refresh interval is now every %dmin", m_refreshInterval);
64 65 66 67 68 69 70 71 72 73 74
  }
}

void RssManager::loadStreamList() {
  RssSettings settings;
  const QStringList streamsUrl = settings.getRssFeedsUrls();
  const QStringList aliases =  settings.getRssFeedsAliases();
  if(streamsUrl.size() != aliases.size()){
    std::cerr << "Corrupted Rss list, not loading it\n";
    return;
  }
75 76
  uint i = 0;
  qDebug() << Q_FUNC_INFO << streamsUrl;
77
  foreach(QString s, streamsUrl){
78
    QStringList path = s.split("\\", QString::SkipEmptyParts);
79
    if(path.empty()) continue;
80 81
    const QString feed_url = path.takeLast();
    qDebug() << "Feed URL:" << feed_url;
82 83
    // Create feed path (if it does not exists)
    RssFolder * feed_parent = this;
84 85
    foreach(const QString &folder_name, path) {
      qDebug() << "Adding parent folder:" << folder_name;
86 87 88
      feed_parent = feed_parent->addFolder(folder_name);
    }
    // Create feed
89
    qDebug() << "Adding feed to parent folder";
90
    RssFeed *stream = feed_parent->addStream(feed_url);
91
    const QString alias = aliases.at(i);
92 93 94 95 96 97 98 99
    if(!alias.isEmpty()) {
      stream->rename(alias);
    }
    ++i;
  }
  qDebug("NB RSS streams loaded: %d", streamsUrl.size());
}

100 101
void RssManager::forwardFeedInfosChanged(const QString &url, const QString &display_name, uint nbUnread) {
  emit feedInfosChanged(url, display_name, nbUnread);
102 103
}

104
void RssManager::forwardFeedIconChanged(const QString &url, const QString &icon_path) {
105 106 107
  emit feedIconChanged(url, icon_path);
}

108 109
void RssManager::moveFile(IRssFile* file, RssFolder* dest_folder) {
  RssFolder* src_folder = file->parent();
110 111
  if(dest_folder != src_folder) {
    // Remove reference in old folder
112 113 114
    src_folder->takeChild(file->id());
    // add to new Folder
    dest_folder->addFile(file);
115 116 117 118 119
  } else {
    qDebug("Nothing to move, same destination folder");
  }
}

120
void RssManager::saveStreamList() const {
121 122 123 124
  QStringList streamsUrl;
  QStringList aliases;
  const QList<RssFeed*> streams = getAllFeeds();
  foreach(const RssFeed *stream, streams) {
125
    QString stream_path = stream->pathHierarchy().join("\\");
126 127 128 129 130
    if(stream_path.isNull()) {
      stream_path = "";
    }
    qDebug("Saving stream path: %s", qPrintable(stream_path));
    streamsUrl << stream_path;
131
    aliases << stream->displayName();
132 133 134 135 136 137
  }
  RssSettings settings;
  settings.setRssFeedsUrls(streamsUrl);
  settings.setRssFeedsAliases(aliases);
}

138
void RssManager::insertSortElem(QList<RssArticle> &list, const RssArticle &item) {
139
  int i = 0;
140
  while(i < list.size() && item.date() < list.at(i).date()) {
141 142 143 144 145
    ++i;
  }
  list.insert(i, item);
}

146 147 148
QList<RssArticle> RssManager::sortNewsList(const QList<RssArticle>& news_list) {
  QList<RssArticle> new_list;
  foreach(const RssArticle &item, news_list) {
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    insertSortElem(new_list, item);
  }
  return new_list;
}

RssManager * RssManager::instance()
{
  if(!m_instance)
    m_instance = new RssManager;
  return m_instance;
}

void RssManager::drop()
{
  if(m_instance) {
    delete m_instance;
    m_instance = 0;
  }
}