summaryrefslogtreecommitdiffstats
path: root/quanta/treeviews/doctreeview.cpp
blob: 288bf4cc531cd6275a60f4035eeb2676f3aa172d (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 <tqstrlist.h>
#include <tqheader.h>
#include <tqpixmap.h>
#include <tqdir.h>

// KDE clases
#include <tdeconfig.h>
#include <kapplication.h>
#include <klocale.h>
#include <tdepopupmenu.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kurl.h>

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

DocTreeView::DocTreeView(TQWidget *parent, const char *name )
  : TDEListView(parent,name)
{

  contextHelpDict = new TQDict<TQString>( 101, false );

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

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

  projectDocFolder = new TDEListViewItem(this, i18n("Project Documentation"));
  projectDocFolder->setOpen(true);
  slotRefreshTree();
  setFocusPolicy(TQ_ClickFocus);

  connect(this, TQT_SIGNAL(executed(TQListViewItem *)), TQT_SLOT(clickItem(TQListViewItem *)) );
  connect(this, TQT_SIGNAL(returnPressed(TQListViewItem *)), TQT_SLOT(clickItem(TQListViewItem *)));
  connect(this, TQT_SIGNAL(doubleClicked(TQListViewItem *)), TQT_SLOT(slotDoubleClicked(TQListViewItem *)));

  m_contextMenu  = new TDEPopupMenu(this);
  m_menuReload = m_contextMenu->insertItem(i18n("&Reload"), this, TQT_SLOT(slotReloadProjectDocs()));
  m_contextMenu->insertItem(SmallIcon("network"), i18n("&Download Documentation..."), this, TQT_SIGNAL(downloadDoc()));
  connect(this, TQT_SIGNAL(contextMenu(TDEListView*, TQListViewItem*, const TQPoint&)),
          this, TQT_SLOT(slotMenu(TDEListView*, TQListViewItem*, const TQPoint&)));
}


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

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

  for ( TQStringList::Iterator it = docDirs.begin(); it != docDirs.end(); ++it )
  {
    TQString docDir = *it;
    TQDir dir(docDir, "*.docrc");
    TQStringList files = dir.entryList();

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

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

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

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

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

}

void DocTreeView::clickItem( TQListViewItem *)
{
  TQListViewItem *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") );
}


TQString * DocTreeView::contextHelp(const TQString &keyword)
{
  TQString 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(TQListViewItem *item )
{
  if (item)
  {
    item->setOpen(!item->isOpen());
  }
}

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

void DocTreeView::slotMenu(TDEListView *, TQListViewItem *item, const TQPoint &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 TQString &, const KURL &, const KURL &)
{
  slotReloadProjectDocs();
}

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

#include "doctreeview.moc"