summaryrefslogtreecommitdiffstats
path: root/quanta/treeviews/doctreeview.cpp
blob: fd9abf65c6d039fc05396891bf13e4569354553b (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
/***************************************************************************
                          doctreeview.cpp  -  description
                             -------------------
    begin                : Sat Mar 4 2000
    copyright            : (C) 2000 by Yacovlev Alexander & Dmitry Poplavsky <pdima@mail.univ.kiev.ua>
                           (C) 2002, 2004 Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// QT clases
#include <qstrlist.h>
#include <qheader.h>
#include <qpixmap.h>
#include <qdir.h>

// KDE clases
#include <kconfig.h>
#include <kapplication.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kurl.h>

// application clases
#include "doctreeview.h"
#include "docfolder.h"
#include "docitem.h"

DocTreeView::DocTreeView(QWidget *parent, const char *name )
  : KListView(parent,name)
{

  contextHelpDict = new QDict<QString>( 101, false );

  setRootIsDecorated( true );
  header()->hide();
  setSorting(-1,false);

  setFrameStyle( Panel | Sunken );
  setLineWidth( 2 );
  addColumn(i18n("Name"), -1);
  addColumn("");
  setFullWidth(true);

  projectDocFolder = new KListViewItem(this, i18n("Project Documentation"));
  projectDocFolder->setOpen(true);
  slotRefreshTree();
  setFocusPolicy(QWidget::ClickFocus);

  connect(this, SIGNAL(executed(QListViewItem *)), SLOT(clickItem(QListViewItem *)) );
  connect(this, SIGNAL(returnPressed(QListViewItem *)), SLOT(clickItem(QListViewItem *)));
  connect(this, SIGNAL(doubleClicked(QListViewItem *)), SLOT(slotDoubleClicked(QListViewItem *)));

  m_contextMenu  = new KPopupMenu(this);
  m_menuReload = m_contextMenu->insertItem(i18n("&Reload"), this, SLOT(slotReloadProjectDocs()));
  m_contextMenu->insertItem(SmallIcon("network"), i18n("&Download Documentation..."), this, SIGNAL(downloadDoc()));
  connect(this, SIGNAL(contextMenu(KListView*, QListViewItem*, const QPoint&)),
          this, SLOT(slotMenu(KListView*, QListViewItem*, const QPoint&)));
}


DocTreeView::~DocTreeView(){
  contextHelpDict->setAutoDelete(true);
  delete contextHelpDict;
}

void DocTreeView::slotRefreshTree()
{
  for (QValueList<DocFolder *>::Iterator it = m_folderList.begin(); it != m_folderList.end(); ++it)
  {
    delete *it;
  }
  m_folderList.clear();
  QStringList docDirs = KGlobal::instance()->dirs()->findDirs("appdata", "doc");

  for ( QStringList::Iterator it = docDirs.begin(); it != docDirs.end(); ++it )
  {
    QString docDir = *it;
    QDir dir(docDir, "*.docrc");
    QStringList files = dir.entryList();

    for ( QStringList::Iterator it_f = files.begin(); it_f != files.end(); ++it_f )
    {
      KConfig config( docDir + *it_f );
      config.setGroup("Tree");

      QString relDocDir = config.readEntry("Doc dir");
      QString name = config.readEntry("Name").lower();

      DocFolder *folder = new DocFolder(this, config.readEntry("Top Element"), &config , QDir::cleanDirPath(docDir+relDocDir)+"/");
      folder->setPixmap( 0, SmallIcon("folder_open") );
      folder->topLevel = true;
      folder->setOpen(true);
      m_folderList.append(folder);

      config.setGroup("Context");
      QStrList list;
      config.readListEntry("ContextList", list );

      for ( unsigned int i=0; i<list.count(); i++ )
      {
        QString keyword = list.at(i);
        QString *url = new QString(QDir::cleanDirPath(docDir + relDocDir + "/" + config.readEntry( list.at(i) )));
        contextHelpDict->insert( name + "|" + keyword, url );
      }
    }
  }

}

void DocTreeView::clickItem( QListViewItem *)
{
  QListViewItem *it = currentItem();
  if ( !it )
    return;
  DocItem *dit = dynamic_cast< DocItem *>(it);
  if ( dit )
    if ( ! dit->url.isEmpty() )
        emit openURL( dit->url);

  DocFolder *dfol = dynamic_cast< DocFolder *>(it);
  if ( dfol )
    if ( ! dfol->url.isEmpty() )
      emit openURL( dfol->url );
  //else
  //  emit openURL( locate("appdata","doc/documentation.html") );
}


QString * DocTreeView::contextHelp(const QString &keyword)
{
  QString word = keyword.mid(keyword.find("|"));
  if (contextHelpDict->find(keyword))
    return contextHelpDict->find(keyword);
  else
    return contextHelpDict->find(word); //to support old documentation packages
}

void DocTreeView::slotDoubleClicked(QListViewItem *item )
{
  if (item)
  {
    item->setOpen(!item->isOpen());
  }
}

void DocTreeView::slotAddProjectDoc(const KURL& url)
{
  QString path = url.path();
  int pos = path.find("/doc/");
  path = path.mid(pos + 5);
  new DocItem(projectDocFolder, path, url.url());
}

void DocTreeView::slotMenu(KListView *, QListViewItem *item, const QPoint &point)
{
  m_contextMenu->setItemVisible(m_menuReload, false);
  if (item)
  {
    setSelected(item, true);
    if (currentItem() == projectDocFolder)
    {
      m_contextMenu->setItemVisible(m_menuReload, true);
    }
  }
  m_contextMenu->popup(point);
}

void DocTreeView::slotNewProjectLoaded(const QString &, const KURL &, const KURL &)
{
  slotReloadProjectDocs();
}

void DocTreeView::slotReloadProjectDocs()
{
  QListViewItem *child = projectDocFolder->firstChild();
  while (child) {
      QListViewItem *c = child;
      child = child->nextSibling();
      delete c;
  }
  emit reloadProjectDocs();
}

#include "doctreeview.moc"