akregator/src

dragobjects.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2005 Frank Osterfeld <frank.osterfeld@kdemail.net>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include "dragobjects.h"
26 #include "feed.h"
27 
28 #include <tqcstring.h>
29 
30 namespace Akregator {
31 
32 class Article;
33 
34 ArticleDrag::ArticleDrag(const TQValueList<Article>& articles, TQWidget* dragSource, const char* name)
35 : KURLDrag(articleURLs(articles), dragSource, name), m_items(articlesToDragItems(articles))
36 {}
37 
38 bool ArticleDrag::canDecode(const TQMimeSource* e)
39 {
40  return e->provides("akregator/articles");
41 }
42 
43 bool ArticleDrag::decode(const TQMimeSource* e, TQValueList<ArticleDragItem>& articles)
44 {
45  articles.clear();
46  TQByteArray array = e->encodedData("akregator/articles");
47 
48  TQDataStream stream(array, IO_ReadOnly);
49 
50  while (!stream.atEnd())
51  {
52  ArticleDragItem i;
53  stream >> i.feedURL;
54  stream >> i.guid;
55  articles.append(i);
56  }
57 
58  return true;
59 }
60 
61 const char* ArticleDrag::format(int i) const
62 {
63  if (i == 0)
64  return "text/uri-list";
65  else if (i == 1)
66  return "akregator/articles";
67 
68  return 0;
69 }
70 
71 TQByteArray ArticleDrag::encodedData(const char* mime) const
72 {
73  TQCString mimetype(mime);
74  if (mimetype == "akregator/articles")
75  {
76  TQByteArray ba;
77  TQDataStream stream(ba, IO_WriteOnly);
78 
79  TQValueList<ArticleDragItem>::ConstIterator end = m_items.end();
80  for (TQValueList<ArticleDragItem>::ConstIterator it = m_items.begin(); it != end; ++it)
81  {
82  stream << (*it).feedURL;
83  stream << (*it).guid;
84  }
85  return ba;
86  }
87  else
88  {
89  return KURLDrag::encodedData(mime);
90  }
91 }
92 
93 TQValueList<ArticleDragItem> ArticleDrag::articlesToDragItems(const TQValueList<Article>& articles)
94 {
95  TQValueList<ArticleDragItem> items;
96 
97  TQValueList<Article>::ConstIterator end(articles.end());
98 
99  for (TQValueList<Article>::ConstIterator it = articles.begin(); it != end; ++it)
100  {
101  ArticleDragItem i;
102  i.feedURL = (*it).feed() ? (*it).feed()->xmlUrl() : "";
103  i.guid = (*it).guid();
104  items.append(i);
105  }
106 
107  return items;
108 }
109 
110 KURL::List ArticleDrag::articleURLs(const TQValueList<Article>& articles)
111 {
112  KURL::List urls;
113  TQValueList<Article>::ConstIterator end(articles.end());
114  for (TQValueList<Article>::ConstIterator it = articles.begin(); it != end; ++it)
115  urls.append((*it).link());
116  return urls;
117 }
118 
119 } // namespace Akregator