progressmanager.cpp
00001 /* 00002 This file is part of Akregator. 00003 00004 Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 00020 As a special exception, permission is given to link this program 00021 with any edition of TQt, and distribute the resulting executable, 00022 without including the source code for TQt in the source distribution. 00023 */ 00024 00025 #include <tqmap.h> 00026 #include <tqstylesheet.h> 00027 00028 #include <tdelocale.h> 00029 #include <kstaticdeleter.h> 00030 00031 #include <libtdepim/progressmanager.h> 00032 00033 #include "feedlist.h" 00034 #include "feed.h" 00035 #include "treenode.h" 00036 00037 #include "progressmanager.h" 00038 00039 //#include <kdebug.h> 00040 00041 namespace Akregator { 00042 00043 class ProgressManager::ProgressManagerPrivate 00044 { 00045 public: 00046 FeedList* feedList; 00047 TQMap<Feed*, ProgressItemHandler*> handlers; 00048 00049 }; 00050 00051 static KStaticDeleter<ProgressManager> progressmanagersd; 00052 ProgressManager* ProgressManager::m_self = 0; 00053 00054 ProgressManager* ProgressManager::self() 00055 { 00056 if (!m_self) 00057 m_self = progressmanagersd.setObject(m_self, new ProgressManager); 00058 return m_self; 00059 } 00060 00061 ProgressManager::ProgressManager() : d(new ProgressManagerPrivate) 00062 { 00063 d->feedList = 0; 00064 } 00065 00066 ProgressManager::~ProgressManager() 00067 { 00068 delete d; 00069 d = 0; 00070 } 00071 00072 void ProgressManager::setFeedList(FeedList* feedList) 00073 { 00074 if (feedList == d->feedList) 00075 return; 00076 00077 if (d->feedList != 0) 00078 { 00079 for (TQMap<Feed*, ProgressItemHandler*>::ConstIterator it = d->handlers.begin(); it != d->handlers.end(); ++it) 00080 delete *it; 00081 d->handlers.clear(); 00082 00083 disconnect(d->feedList, TQT_SIGNAL(signalNodeAdded(TreeNode*)), this, TQT_SLOT(slotNodeAdded(TreeNode*))); 00084 disconnect(d->feedList, TQT_SIGNAL(signalNodeRemoved(TreeNode*)), this, TQT_SLOT(slotNodeRemoved(TreeNode*))); 00085 } 00086 00087 d->feedList = feedList; 00088 00089 if (feedList != 0) 00090 { 00091 TQValueList<TreeNode*> list = feedList->asFlatList(); 00092 00093 for (TQValueList<TreeNode*>::ConstIterator it = list.begin(); it != list.end(); ++it) 00094 slotNodeAdded(*it); 00095 connect(feedList, TQT_SIGNAL(signalNodeAdded(TreeNode*)), this, TQT_SLOT(slotNodeAdded(TreeNode*))); 00096 connect(feedList, TQT_SIGNAL(signalNodeRemoved(TreeNode*)), this, TQT_SLOT(slotNodeRemoved(TreeNode*))); 00097 } 00098 } 00099 00100 void ProgressManager::slotNodeAdded(TreeNode* node) 00101 { 00102 Feed* feed = dynamic_cast<Feed*>(node); 00103 if (feed) 00104 { 00105 if (!d->handlers.contains(feed)) 00106 d->handlers[feed] = new ProgressItemHandler(feed); 00107 connect(feed, TQT_SIGNAL(signalDestroyed(TreeNode*)), this, TQT_SLOT(slotNodeDestroyed(TreeNode*))); 00108 } 00109 } 00110 00111 void ProgressManager::slotNodeRemoved(TreeNode* node) 00112 { 00113 Feed* feed = dynamic_cast<Feed*>(node); 00114 if (feed) 00115 { 00116 disconnect(feed, TQT_SIGNAL(signalDestroyed(TreeNode*)), this, TQT_SLOT(slotNodeDestroyed(TreeNode*))); 00117 delete d->handlers[feed]; 00118 d->handlers.remove(feed); 00119 } 00120 } 00121 00122 void ProgressManager::slotNodeDestroyed(TreeNode* node) 00123 { 00124 Feed* feed = dynamic_cast<Feed*>(node); 00125 if (feed) 00126 { 00127 delete d->handlers[feed]; 00128 d->handlers.remove(feed); 00129 } 00130 } 00131 00132 class ProgressItemHandler::ProgressItemHandlerPrivate 00133 { 00134 public: 00135 00136 Feed* feed; 00137 KPIM::ProgressItem* progressItem; 00138 }; 00139 00140 ProgressItemHandler::ProgressItemHandler(Feed* feed) : d(new ProgressItemHandlerPrivate) 00141 { 00142 d->feed = feed; 00143 d->progressItem = 0; 00144 00145 connect(feed, TQT_SIGNAL(fetchStarted(Feed*)), this, TQT_SLOT(slotFetchStarted())); 00146 connect(feed, TQT_SIGNAL(fetched(Feed*)), this, TQT_SLOT(slotFetchCompleted())); 00147 connect(feed, TQT_SIGNAL(fetchError(Feed*)), this, TQT_SLOT(slotFetchError())); 00148 connect(feed, TQT_SIGNAL(fetchAborted(Feed*)), this, TQT_SLOT(slotFetchAborted())); 00149 } 00150 00151 ProgressItemHandler::~ProgressItemHandler() 00152 { 00153 if (d->progressItem) 00154 { 00155 d->progressItem->setComplete(); 00156 d->progressItem = 0; 00157 } 00158 00159 delete d; 00160 d = 0; 00161 } 00162 00163 void ProgressItemHandler::slotFetchStarted() 00164 { 00165 if (d->progressItem) 00166 { 00167 d->progressItem->setComplete(); 00168 d->progressItem = 0; 00169 } 00170 00171 d->progressItem = KPIM::ProgressManager::createProgressItem(KPIM::ProgressManager::getUniqueID(), TQStyleSheet::escape( d->feed->title() ), TQString(), true); 00172 00173 connect(d->progressItem, TQT_SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), d->feed, TQT_SLOT(slotAbortFetch())); 00174 } 00175 00176 00177 void ProgressItemHandler::slotFetchCompleted() 00178 { 00179 if (d->progressItem) 00180 { 00181 d->progressItem->setStatus(i18n("Fetch completed")); 00182 d->progressItem->setComplete(); 00183 d->progressItem = 0; 00184 } 00185 } 00186 00187 void ProgressItemHandler::slotFetchError() 00188 { 00189 if (d->progressItem) 00190 { 00191 d->progressItem->setStatus(i18n("Fetch error")); 00192 d->progressItem->setComplete(); 00193 d->progressItem = 0; 00194 } 00195 } 00196 00197 void ProgressItemHandler::slotFetchAborted() 00198 { 00199 if (d->progressItem) 00200 { 00201 d->progressItem->setStatus(i18n("Fetch aborted")); 00202 d->progressItem->setComplete(); 00203 d->progressItem = 0; 00204 } 00205 } 00206 00207 } // namespace Akregator 00208 00209 #include "progressmanager.moc"