summaryrefslogtreecommitdiffstats
path: root/quanta/treeviews/filestreeview.cpp
blob: bebac21df9c5f4d2948268fabbc6345f1e56635c (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/***************************************************************************
                          filestreeview.cpp  -  description
                             -------------------
    begin                : Thu Jun 1 2000
    copyright            : (C) 2000 by Dmitry Poplavsky & Alexander Yakovlev & Eric Laffoon <pdima@users.sourceforge.net,yshurik@linuxfan.com,sequitur@easystreet.com>
                           (C) 2001-2004 Andras Mantia <amantoa@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.                                   *
 *                                                                         *
 ***************************************************************************/


// KDE includes
#include <kdebug.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <kopenwith.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <kfiledialog.h>
#include <kprogress.h>
#include <kurl.h>

// app includes
#include "filestreeview.h"
#include "resource.h"
#include "project.h"
#ifdef ENABLE_CVSSERVICE
#include "cvsservice.h"
#endif
#include "qextfileinfo.h"


//FilesTreeView implementation

FilesTreeView::FilesTreeView(KConfig *config, TQWidget *parent, const char *name)
  : BaseTreeView(parent, name)
{
  setAcceptDrops(true);
  setDragEnabled(true);
  setSaveOpenFolder(true);

  m_config = config;
  m_config->setGroup("General Options");
  // I must read this here because quanta_init has not done it yet
  qConfig.showHiddenFiles = m_config->readBoolEntry("Show Hidden Files", true);
  qConfig.saveTrees = m_config->readBoolEntry("Save Local Trees", true);
  TQStringList topStrList = QuantaCommon::readPathListEntry(m_config, "Top folders");
  TQStringList topStrAliasList = QuantaCommon::readPathListEntry(m_config, "Top folder aliases");
  KURL url;
  for (uint i = 0; i < topStrList.count(); i++)
  {
    url = KURL();
    QuantaCommon::setUrl(url, topStrList[i]);
    if (!topURLList.contains(url))
    {
      topURLList.append(url);
      if (i < topStrAliasList.count())
        topURLAliases.insert(url.url(), topStrAliasList[i]);
      else
        topURLAliases.insert(url.url(), url.fileName());
    }
  }

  m_config->setGroup("General Options");
  if (m_config->readBoolEntry("Home-Root Folder On", true))
  {
    url = KURL();
    url.setPath("/");
    if (!topURLList.contains(url))
        topURLList.append(url);
    url = KURL();
    url.setPath(QExtFileInfo::homeDirPath() + "/");
    if (!topURLList.contains(url))
        topURLList.append(url);
  }

  m_fileMenu = new KPopupMenu(this);

  m_fileMenu->insertItem(SmallIcon("fileopen"), i18n("&Open"), this ,TQT_SLOT(slotOpen()));
  m_fileMenu->insertItem(i18n("Insert &Tag"), this, TQT_SLOT(slotInsertTag()));
  m_menuClose = m_fileMenu->insertItem(SmallIcon("fileclose"), i18n("Clos&e"), this, TQT_SLOT(slotClose()));
  m_fileMenu->insertSeparator();
  m_insertFileInProject = m_fileMenu->insertItem(i18n("&Insert in Project..."), this, TQT_SLOT(slotInsertInProject()));
  m_fileMenu->insertItem(SmallIcon("editcopy"), i18n("&Copy"), this, TQT_SLOT(slotCopy()));
  m_fileMenu->insertItem(i18n("Re&name"), this, TQT_SLOT(slotStartRename()));
  m_fileMenu->insertItem(SmallIcon("editdelete"), i18n("&Delete"), this, TQT_SLOT(slotDelete()));
  m_fileMenu->insertSeparator();
  m_fileMenu->insertItem(SmallIcon("info"), i18n("&Properties"), this, TQT_SLOT(slotProperties()));

  KPopupMenu *createNewMenu = new KPopupMenu(this);
  createNewMenu->insertItem(SmallIcon("folder_new"), i18n("F&older..."), this, TQT_SLOT(slotCreateFolder()));
  createNewMenu->insertItem(SmallIcon("document"), i18n("&File..."), this, TQT_SLOT(slotCreateFile()));

  m_folderMenu = new KPopupMenu();

  m_folderMenu->insertItem(SmallIcon("folder_new"), i18n("New Top &Folder..."), this, TQT_SLOT(slotNewTopFolder()));
  m_menuTop = m_folderMenu->insertItem(i18n("&Add Folder to Top"), this, TQT_SLOT(slotAddToTop()));
  m_folderMenu->insertItem(i18n("Create Site &Template..."), this, TQT_SLOT(slotCreateSiteTemplate()));
  m_folderMenu->insertItem(SmallIcon("empty"), i18n("&Create New"), createNewMenu);
  m_folderMenu->insertSeparator();
  m_insertFolderInProject = m_folderMenu->insertItem(i18n("&Insert in Project..."), this, TQT_SLOT(slotInsertDirInProject()));
  m_folderMenu->insertItem(SmallIcon("editcopy"), i18n("&Copy"), this, TQT_SLOT(slotCopy()));
  m_menuPasteFolder = m_folderMenu->insertItem(SmallIcon("editpaste"), i18n("&Paste"), this, TQT_SLOT(slotPaste()));
  m_menuChangeAlias = m_folderMenu->insertItem(i18n("&Change Alias..."), this, TQT_SLOT(slotChangeAlias()));
  m_menuFolderRename = m_folderMenu->insertItem(i18n("Re&name"), this, TQT_SLOT(slotStartRename()));
  m_menuDel = m_folderMenu->insertItem( SmallIcon("editdelete"), i18n("&Delete"), this, TQT_SLOT(slotDelete()));
  m_folderMenu->insertSeparator();
  m_folderMenu->insertItem(SmallIcon("info"), i18n("&Properties"), this, TQT_SLOT(slotProperties()));
  m_reloadMenuId = m_folderMenu->insertItem(SmallIcon("revert"), i18n("&Reload"), this, TQT_SLOT(slotReload()));

  m_emptyMenu = new KPopupMenu();

  m_emptyMenu->insertItem(i18n("New Top &Folder..."), this, TQT_SLOT(slotNewTopFolder()), 0, -1 , 0);

  addColumn(i18n("Files Tree"), -1);
  addColumn("");

  connect(this, TQT_SIGNAL(open(TQListViewItem *)),
          this, TQT_SLOT(slotSelectFile(TQListViewItem *)));
  connect(this, TQT_SIGNAL(contextMenu(KListView*, TQListViewItem*, const TQPoint&)),
          this, TQT_SLOT(slotMenu(KListView*, TQListViewItem*, const TQPoint&)));

  // generate top list of directories
  for (uint i = 0; i <  topURLList.count(); i++)
  {
    KURL url = topURLList[i];
    newBranch(url);
  }
  restoreLayout(m_config, className());
  // the restored size of the first column might be too large for the current content
  // we set it to 10 and the listview will adjust it to the size of the largest entry
  setColumnWidth(0, 10);
}

FilesTreeView::~FilesTreeView()
{
}

KFileTreeBranch* FilesTreeView::newBranch(const KURL& url)
{
  BaseTreeBranch* newBrnch = 0;
  KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, url);
  if (url.isLocalFile() && url.path() == "/")
  {
    newBrnch = new BaseTreeBranch(this, url, i18n("Root Folder"), SmallIcon(fileItem.iconName()), true);
  } else
  {
    if (url.isLocalFile() && url.equals(KURL(QExtFileInfo::homeDirPath() + "/"), true))
    {
      newBrnch = new BaseTreeBranch(this, url, i18n("Home Folder"), SmallIcon(fileItem.iconName()), true);
    } else
    {
      TQString s = url.fileName();
      if (s.isEmpty())
        s = "/";
      if (topURLAliases.contains(url.url()))
        s = topURLAliases[url.url()];
      s += " [" + url.prettyURL() + "]";
      newBrnch = new BaseTreeBranch(this, url, s, SmallIcon(fileItem.iconName()), true);
    }
  }
  newBrnch->setShowingDotFiles(qConfig.showHiddenFiles);
  addBranch(newBrnch);
  return newBrnch;
}



/** RMB pressed, bring up the menu */
void FilesTreeView::slotMenu(KListView* listView, TQListViewItem *item, const TQPoint &point)
{
  Q_UNUSED(listView);
#ifdef ENABLE_CVSSERVICE
  KURL url = currentURL();
  if (Project::ref()->contains(url))
    CVSService::ref()->setRepository(Project::ref()->projectBaseURL().path());
  else
    CVSService::ref()->setRepository(url.directory());
  if (url.isLocalFile())
  {
    CVSService::ref()->setCurrentFile(url.path());
  }
  else
    CVSService::ref()->setCurrentFile("");
#endif
  if (item)
  {
    bool hasProject = !m_projectName.isNull();
    m_folderMenu->setItemVisible(m_menuChangeAlias, false);
    m_folderMenu->setItemVisible(m_menuFolderRename, true);
    m_folderMenu->setItemVisible(m_insertFolderInProject, hasProject);
    m_fileMenu->setItemVisible(m_insertFileInProject, hasProject);
    setSelected(item, true);
    KFileTreeViewItem *curItem = currentKFileTreeViewItem();
    if ( !curItem->isDir() )
    {
      m_fileMenu->setItemVisible(m_menuClose, isFileOpen(currentURL()));
      insertOpenWithMenu(m_fileMenu, 1);
      m_fileMenu->popup( point);
    } else {
      m_folderMenu->setItemVisible( m_menuDel, true );
      m_folderMenu->setItemVisible( m_menuTop, true );
      m_folderMenu->setItemVisible(m_menuPasteFolder, isPathInClipboard());
      KURL url = curItem->url();
      if ( curItem == curItem->branch()->root() )
      {
        m_folderMenu->setItemVisible(m_menuDel, false);
        m_folderMenu->changeItem(m_menuTop, i18n("Remove From &Top"));
        m_folderMenu->setItemVisible(m_menuChangeAlias, true);
        m_folderMenu->setItemVisible(m_menuFolderRename, false);

        m_config->setGroup("General Options");
        if ((url == KURL("file:/") || url == KURL("file:" + QExtFileInfo::homeDirPath() + "/")) &&
            m_config->readBoolEntry("Home-Root Folder On", true) )
          m_folderMenu ->setItemVisible(m_menuTop, false);
        m_folderMenu ->setItemVisible(m_reloadMenuId, true);
      }
      else
      {
        m_folderMenu->changeItem( m_menuTop, i18n("&Add Folder to Top"));
        url.adjustPath(+1);
        m_folderMenu->setItemVisible(m_menuTop, (topURLList.findIndex(url) == -1));
        m_folderMenu->setItemVisible(m_reloadMenuId, false);
      }
      m_folderMenu->popup( point);
   }
 } else {
   m_emptyMenu->popup( point );
 }
}



/** Add or remove folders to/from the top list */
void FilesTreeView::slotAddToTop()
{
  KFileTreeViewItem *curItem = currentKFileTreeViewItem();
  if ( !curItem ) return;

  if ( curItem->isDir() )
  {
    KURL url(currentURL().url());
    url.adjustPath(+1);
    if ( curItem != curItem->branch()->root() )  //it is not a top folder
    { // add
      if (topURLList.findIndex(url) == -1)
      {
        topURLList.append(url);
        bool ok;
        TQString aliasName = KInputDialog::getText(i18n("Set Alias"), i18n("Alternative folder name:"), url.fileName(), &ok, this);
        if (!ok)
          aliasName = url.fileName();
        topURLAliases.insert(url.url(), aliasName);
        newBranch(url);
      } else {
        KMessageBox::information(this, i18n("<qt><b>%1</b> is already a toplevel entry.</qt>").tqarg(url.url()));
      }
    } else
    { // remove
      topURLList.remove(url);
      topURLAliases.remove(url.url());
      removeBranch(curItem->branch());
    }
  }
}

void FilesTreeView::slotNewTopFolder()
{
  KURL url = KFileDialog::getExistingURL(TQString(), this, i18n("Choose Local or Remote Folder"));
  if (url.isEmpty()) return;
  url.adjustPath(+1);
  if (topURLList.findIndex(url) == -1)
  {
    bool ok;
    TQString aliasName = KInputDialog::getText(i18n("Set Alias"), i18n("Alternative folder name:"), url.fileName(), &ok, this);
    if (!ok)
      aliasName = url.fileName();
    topURLAliases.insert(url.url(), aliasName);
    newBranch(url);
    topURLList.append(url);
  } else {
    KMessageBox::information(this, i18n("<qt><b>%1</b> is already a toplevel entry.</qt>").tqarg(url.url()));
  }
}


void FilesTreeView::plugCVSMenu()
{
#ifdef ENABLE_CVSSERVICE
  m_fileMenu->insertSeparator();
  m_fileMenu->insertItem(SmallIcon("cervisia"), i18n("C&VS"), CVSService::ref()->menu());
  m_folderMenu->insertSeparator();
  m_folderMenu->insertItem(SmallIcon("cervisia"), i18n("C&VS"), CVSService::ref()->menu());
#endif
}

void FilesTreeView::slotChangeAlias()
{
  KFileTreeViewItem *curItem = currentKFileTreeViewItem();
  if ( !curItem ) return;

  if ( curItem->isDir() )
  {
    KURL url(currentURL().url());
    url.adjustPath(+1);
    if ( curItem == curItem->branch()->root() )  //it is not a top folder
    {
      if (topURLList.findIndex(url) != -1)
      {
        bool ok;
        TQString aliasName = KInputDialog::getText(i18n("Change Alias"), i18n("Alternative folder name:"), topURLAliases[url.url()], &ok, this);
        if (ok)
        {
          topURLAliases.replace(url.url(), aliasName);
          removeBranch(curItem->branch());
          newBranch(url);
        }
      } else {
        KMessageBox::information(this, i18n("<qt><b>%1</b> is already a toplevel entry.</qt>").tqarg(url.url()));
      }
    }
  }
}


#include "filestreeview.moc"