summaryrefslogtreecommitdiffstats
path: root/konq-plugins/sidebar/newsticker/sidebar_news.cpp
blob: 97f24e363c0de67a840d50fc4254d0a8490d0e0a (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8; -*-

/***************************************************************************
                          sidebar_news.cpp - The real sidebar plugin
                             -------------------
    begin                 : Sat June 23 13:35:30 CEST 2001
    copyright             : (C) 2001,2002 Marcus Camen, Joseph Wenninger
    copyright             : (C) 2003,2004 Marcus Camen
    email                 : Marcus Camen <mcamen@mcamen.de>
    idea and original code: jowenn@kde.org
***************************************************************************/

/*
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include <dcopclient.h>
#include <tqtimer.h>
#include <tqbuffer.h>
#include <tqwidgetstack.h>
#include <kdebug.h>
#include <kapplication.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdesktopfile.h>
#include <kiconloader.h>
#include <kdemacros.h>
#include "sidebar_news.h"
#include "nsstacktabwidget.h"
#include "norsswidget.h"
#include "sidebarsettings.h"


namespace KSB_News {

  KonqSidebar_News::KonqSidebar_News(KInstance *inst, TQObject *tqparent,
                                     TQWidget *widgetParent,
                                     TQString &desktopName, const char* name)
    : KonqSidebarPlugin(inst, tqparent, widgetParent, desktopName, name),
      DCOPObject("sidebar-newsticker")
  {
    // get the application icon
    // FIXME: as konqueror knows the icon there might be a possibility to
    //        access the already present TQPixmap
    KDesktopFile desktopFile(desktopName, true);
    TQString iconName = desktopFile.readIcon();
    KIconLoader iconLoader;
    m_appIcon = iconLoader.loadIcon(iconName, KIcon::Small);

    // create all sidebar widgets
    widgets = new TQWidgetStack(widgetParent, "main_widgetstack");
    newswidget = new NSStackTabWidget(widgets, "feedbrowser_stackchld",
                                      m_appIcon);
    noRSSwidget = new NoRSSWidget(widgets, "nofeed_stackchld");
    widgets->addWidget(newswidget);
    widgets->addWidget(noRSSwidget);
    widgets->raiseWidget(noRSSwidget);
    noRSSwidget->show();

    // try to connect to the DCOP service
    if (checkDcopService() > 0) {
      KMessageBox::sorry(widgets,
                         i18n("<qt>Cannot connect to RSS service. Please make "
                              "sure the <strong>rssservice</strong> program "
                              "is available (usually distributed as part "
                              "of kdenetwork).</qt>"),
                         i18n("Sidebar Newsticker"));
      noRSSwidget->setEnabled(false);
    } else {
      m_rssservice = DCOPRef("rssservice", "RSSService");

      TQStringList reslist = SidebarSettings::sources();
      TQStringList::iterator it;
      for (it = reslist.begin(); it != reslist.end(); ++it) {
        addedRSSSource(*it);
      }

      // fetch added and removed RSS sources
      connectDCOPSignal("rssservice", m_rssservice.obj(), "added(TQString)",
                        "addedRSSSource(TQString)", false);
      connectDCOPSignal("rssservice", m_rssservice.obj(), "removed(TQString)",
                        "removedRSSSource(TQString)", false);

      // show special widget if there are no RSS sources available
      if (newswidget->isEmpty()) {
        widgets->raiseWidget(noRSSwidget);
        noRSSwidget->show();
      } else {
        widgets->raiseWidget(newswidget);
      }
    }
  }


  KonqSidebar_News::~KonqSidebar_News() {
  }



  void *KonqSidebar_News::provides(const TQString &) {return 0;}

  void KonqSidebar_News::emitStatusBarText (const TQString &) {;}

  TQWidget *KonqSidebar_News::getWidget(){return widgets;}

  void KonqSidebar_News::handleURL(const KURL &/*url*/) {;}


///////// startup of the DCOP servce ///////////////////////////////////////

  int KonqSidebar_News::checkDcopService() {
    TQString rdfservice_error;
    int err = 0;

    if (! kapp->dcopClient()->isApplicationRegistered("rssservice"))
      if (KApplication::startServiceByDesktopName("rssservice", TQString(),
                                                  &rdfservice_error) > 0)
        err = 1;

    return err;
  }



///////// helper methods ///////////////////////////////////////////////////

  NSPanel *KonqSidebar_News::getNSPanelByKey(TQString key) {
    NSPanel *nsp = NULL, *current_nsp;

    for (current_nsp = nspanelptrlist.first(); current_nsp;
         current_nsp = nspanelptrlist.next()) {
      if (current_nsp->key() == key)
        nsp = current_nsp;
    }

    return nsp;
  }


  void KonqSidebar_News::addedRSSSource(TQString key) {
    kdDebug(90140) << "KonqSidebar_News::addedRSSSource: " << key << endl;

    // Only add RSS source if we have registered the URI before in
    // NSStackTabWidget.
    if (newswidget->isRegistered(key)) {
      NSPanel *nspanel = new NSPanel(this,
                      TQString(TQString("sidebar-newsticker-")+key).latin1(),
                      key, &m_rssservice);
      nspanel->setTitle(key);
      nspanelptrlist.append(nspanel);

      // add preliminary widgets for this newssource
      if (! nspanel->listbox()) {
        TTListBox *listbox = new TTListBox(newswidget, "article_lb");
        newswidget->addStackTab(nspanel, listbox);
        connect(listbox, TQT_SIGNAL(executed(TQListBoxItem *)),
                this, TQT_SLOT(slotArticleItemExecuted(TQListBoxItem *)));
        listbox->insertItem(i18n("Connecting..." ));
        nspanel->setListbox(listbox);
      }

      // listen to updates
      connect(nspanel, TQT_SIGNAL(documentUpdated(NSPanel *)),
              this, TQT_SLOT(updateArticles(NSPanel *)));
      connect(nspanel, TQT_SIGNAL(documentUpdated(NSPanel *)),
              this, TQT_SLOT(updateTitle(NSPanel *)));
      connect(nspanel, TQT_SIGNAL(pixmapUpdated(NSPanel *)),
              this, TQT_SLOT(updatePixmap(NSPanel *)));

      if (widgets->visibleWidget() != newswidget)
        widgets->raiseWidget(newswidget);
    }
  }


  void KonqSidebar_News::removedRSSSource(TQString key) {
    kdDebug(90140) << "inside KonqSidebar_News::removedSource " << key << endl;

    if (NSPanel *nsp = getNSPanelByKey(key)) {
      newswidget->delStackTab(nsp);
      delete nspanelptrlist.take(nspanelptrlist.tqfindRef(nsp));
    } else
      kdWarning() << "removedSource called for non-existing id" << endl;

    if (newswidget->isEmpty())
      widgets->raiseWidget(noRSSwidget);
  }


/////////////////////////////////////////////////////////////////////

  void KonqSidebar_News::slotArticleItemExecuted(TQListBoxItem *item) {
    if (!item) return;

    NSPanel *current_nspanel, *nspanel = NULL;
    for (current_nspanel = nspanelptrlist.first(); current_nspanel;
         current_nspanel = nspanelptrlist.next()) {
      if (current_nspanel->listbox() == item->listBox())
        nspanel = current_nspanel;
    }

    int subid = nspanel->listbox()->index(item);
    TQString link = nspanel->articleLinks()[subid];

    emit openURLRequest(KURL(link));

  }



//////////// update article headlines ////////////

  void KonqSidebar_News::updateArticles(NSPanel *nsp) {
    nsp->listbox()->clear();

    TQStringList articleList = nsp->articles();
    TQStringList::iterator it;
    for (it = articleList.begin(); it != articleList.end(); ++it)
      nsp->listbox()->insertItem((*it));
  }


/////////// Title stuff /////////////////////////////////////

  void KonqSidebar_News::updateTitle(NSPanel *nsp) {
    newswidget->updateTitle(nsp);
  }


/////////// Pixmap stuff /////////////////////////////////////

  void KonqSidebar_News::updatePixmap(NSPanel *nsp) {
    newswidget->updatePixmap(nsp);
  }



/////////////////////////////////////////////////////////


  extern "C" {
    KDE_EXPORT void* create_konq_sidebarnews(KInstance *instance, TQObject *par,
                                             TQWidget *widp,
                                             TQString &desktopname,
                                             const char *name) {
      KGlobal::locale()->insertCatalogue("konqsidebar_news");
      return new KonqSidebar_News(instance, par, widp, desktopname, name);
    }
  }

  extern "C" {
    KDE_EXPORT bool add_konq_sidebarnews(TQString* fn, TQString*,
                                         TQMap<TQString,TQString> *map) {
      map->insert("Type", "Link");
      map->insert("Icon", "konqsidebar_news");
      map->insert("Name", i18n("Newsticker"));
      map->insert("Open", "false");
      map->insert("X-KDE-KonqSidebarModule", "konq_sidebarnews");
      fn->setLatin1("news%1.desktop");
      return true;
    }
  }

} // namespace KSB_News

#include "sidebar_news.moc"