akregator/src

dragobjects.cpp

00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2005 Frank Osterfeld <frank.osterfeld@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 Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "dragobjects.h"
00026 #include "feed.h"
00027 
00028 #include <qcstring.h>
00029 
00030 namespace Akregator {
00031 
00032 class Article;
00033 
00034 ArticleDrag::ArticleDrag(const QValueList<Article>& articles, QWidget* dragSource, const char* name)
00035 : KURLDrag(articleURLs(articles), dragSource, name), m_items(articlesToDragItems(articles))
00036 {}
00037 
00038 bool ArticleDrag::canDecode(const QMimeSource* e)
00039 {
00040     return e->provides("akregator/articles");
00041 }
00042 
00043 bool ArticleDrag::decode(const QMimeSource* e, QValueList<ArticleDragItem>& articles)
00044 {
00045     articles.clear();
00046     QByteArray array = e->encodedData("akregator/articles");
00047     
00048     QDataStream stream(array, IO_ReadOnly);
00049 
00050     while (!stream.atEnd())
00051     {
00052         ArticleDragItem i;
00053         stream >> i.feedURL;
00054         stream >> i.guid;
00055         articles.append(i);
00056     }
00057 
00058     return true;
00059 }
00060 
00061 const char* ArticleDrag::format(int i) const
00062 {
00063     if (i == 0)
00064         return "text/uri-list";
00065     else if (i == 1)
00066         return "akregator/articles";
00067 
00068     return 0;
00069 }
00070 
00071 QByteArray ArticleDrag::encodedData(const char* mime) const
00072 {
00073     QCString mimetype(mime);
00074     if (mimetype == "akregator/articles")
00075     {
00076         QByteArray ba;
00077         QDataStream stream(ba, IO_WriteOnly);
00078 
00079         QValueList<ArticleDragItem>::ConstIterator end = m_items.end();
00080         for (QValueList<ArticleDragItem>::ConstIterator it = m_items.begin(); it != end; ++it)
00081         {
00082             stream << (*it).feedURL;
00083             stream << (*it).guid;
00084         } 
00085         return ba;
00086     }
00087     else
00088     {
00089         return KURLDrag::encodedData(mime);
00090     }
00091 }
00092 
00093 QValueList<ArticleDragItem> ArticleDrag::articlesToDragItems(const QValueList<Article>& articles)
00094 {
00095     QValueList<ArticleDragItem> items;
00096     
00097     QValueList<Article>::ConstIterator end(articles.end());
00098 
00099     for (QValueList<Article>::ConstIterator it = articles.begin(); it != end; ++it)
00100     {
00101         ArticleDragItem i;
00102         i.feedURL = (*it).feed() ? (*it).feed()->xmlUrl() : "";
00103         i.guid = (*it).guid();
00104         items.append(i);
00105     }
00106 
00107     return items;
00108 }
00109 
00110 KURL::List ArticleDrag::articleURLs(const QValueList<Article>& articles)
00111 {
00112     KURL::List urls;
00113     QValueList<Article>::ConstIterator end(articles.end());
00114     for (QValueList<Article>::ConstIterator it = articles.begin(); it != end; ++it)
00115         urls.append((*it).link());
00116     return urls;
00117 }
00118 
00119 } // namespace Akregator
KDE Home | KDE Accessibility Home | Description of Access Keys