1 /**************************************************************************
2 * Copyright (C) 2005-2020 by Oleksandr Shneyder *
3 * <o.shneyder@phoca-gmbh.de> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17 #include "folderexplorer.h"
18 #include "x2gologdebug.h"
19 #include "sessionexplorer.h"
20 #include "folderbutton.h"
21 #include <QMenu>
22 #include <QFileDialog>
23 #include <QInputDialog>
24 #include <QMessageBox>
25
FolderExplorer(QString path,SessionExplorer * explorer,QWidget * parent)26 FolderExplorer::FolderExplorer(QString path, SessionExplorer* explorer, QWidget* parent):QDialog(parent)
27 {
28 setupUi(this);
29 this->explorer=explorer;
30 root=new QTreeWidgetItem(treeWidget);
31 root->setText(0,"/");
32 root->setIcon(0,QIcon(":/img/icons/128x128/folder.png"));
33 currentPath=path;
34 initFolders(root, "");
35 root->setExpanded(true);
36 if(currentPath=="/")
37 root->setSelected(true);
38 root->setData(0,Qt::UserRole, "/");
39
40 }
41
initFolders(QTreeWidgetItem * parent,QString path)42 void FolderExplorer::initFolders(QTreeWidgetItem* parent, QString path)
43 {
44 FolderButton* b;
45 foreach(b, *(explorer->getFoldersList()))
46 {
47 if(b->getPath()==path)
48 {
49 QTreeWidgetItem* it=new QTreeWidgetItem(parent);
50 it->setText(0,b->getName());
51 it->setIcon(0, QIcon(*(b->folderIcon())));
52 QString normPath=(b->getPath()+"/"+b->getName()).split("/",QString::SkipEmptyParts).join("/");
53 it->setData(0,Qt::UserRole, normPath+"/");
54 if(normPath+"/"==currentPath)
55 {
56
57 it->setSelected(true);
58 QTreeWidgetItem* p=it->parent();
59 while(p!=root)
60 {
61 p->setExpanded(true);
62 p=p->parent();
63 }
64 }
65 initFolders(it, normPath);
66 }
67 }
68 }
69
slotContextMenu(QPoint p)70 void FolderExplorer::slotContextMenu(QPoint p)
71 {
72 menuItem=treeWidget->itemAt(p);
73 if(!menuItem)
74 return;
75
76 QMenu menu(treeWidget);
77 connect(menu.addAction(tr("Create New Folder")), SIGNAL(triggered(bool)), this, SLOT(slotNewFolder()));
78
79 if(menuItem!=root)
80 {
81 connect(menu.addAction(tr("Rename Folder ...")), SIGNAL(triggered(bool)), this, SLOT(slotChangeName()));
82 connect(menu.addAction(tr("Change Icon ...")), SIGNAL(triggered(bool)), this, SLOT(slotChangeIcon()));
83 connect(menu.addAction(tr("Delete Folder ...")), SIGNAL(triggered(bool)), this, SLOT(slotDeleteFolder()));
84 }
85 menu.exec(treeWidget->viewport()->mapToGlobal(p));
86 }
87
slotItemSelected(QTreeWidgetItem * it,int)88 void FolderExplorer::slotItemSelected(QTreeWidgetItem* it, int)
89 {
90 currentPath=it->data(0,Qt::UserRole).toString();
91 }
92
slotChangeIcon()93 void FolderExplorer::slotChangeIcon()
94 {
95 QString path= QFileDialog::getOpenFileName (
96 this,
97 tr ( "Open picture" ),
98 QDir::homePath(),
99 tr ( "Pictures" ) +" (*.png *.xpm *.jpg)" );
100 if ( path!=QString::null )
101 {
102 explorer->setFolderIcon(menuItem->data(0, Qt::UserRole).toString(), path);
103 menuItem->setIcon(0, QIcon(path));
104 }
105 }
106
slotChangeName()107 void FolderExplorer::slotChangeName()
108 {
109 bool ok;
110 QString oldPath=menuItem->data(0,Qt::UserRole).toString();
111 QStringList parts=oldPath.split("/",QString::SkipEmptyParts);
112 QString text = QInputDialog::getText(this, tr("X2Go Client"),
113 tr("Folder Name:"), QLineEdit::Normal,
114 parts.last(), &ok);
115 if (ok && !text.isEmpty())
116 {
117 menuItem->setText(0,text);
118 parts.pop_back();
119 parts<<text;
120 currentPath= parts.join("/")+"/";
121 menuItem->setData(0,Qt::UserRole, currentPath);
122 explorer->renameFolder(oldPath, currentPath);
123 }
124 }
125
slotDeleteFolder()126 void FolderExplorer::slotDeleteFolder()
127 {
128 if(!explorer->isFolderEmpty(menuItem->data(0, Qt::UserRole).toString()))
129 {
130 QMessageBox::critical(this, tr("Error"), tr("Unable to remove \"")+menuItem->text(0)+
131 tr("\". Folder is not empty. Please remove the contents of this directory and try again."));
132 return;
133 }
134 if(QMessageBox::question(this, "X2Go Client", tr("Delete folder \"")+menuItem->text(0)+"\"?",QMessageBox::Ok|QMessageBox::Cancel) == QMessageBox::Ok)
135 {
136 explorer->deleteFolder(menuItem->data(0, Qt::UserRole).toString());
137 currentPath="/";
138 delete menuItem;
139 }
140 }
141
slotNewFolder()142 void FolderExplorer::slotNewFolder()
143 {
144 QTreeWidgetItem* it=new QTreeWidgetItem(menuItem);
145 QString name=tr("New Folder");
146 it->setText(0,name);
147 it->setIcon(0, QIcon(":/img/icons/128x128/folder.png"));
148 QString normPath=(menuItem->data(0,Qt::UserRole).toString()+"/"+name).split("/",QString::SkipEmptyParts).join("/");
149 it->setData(0,Qt::UserRole, normPath+"/");
150 treeWidget->clearSelection();
151 it->setSelected(true);
152 QTreeWidgetItem* p=it->parent();
153 while(p!=root)
154 {
155 p->setExpanded(true);
156 p=p->parent();
157 }
158 slotItemSelected(it,0);
159 explorer->createNewFolder(normPath);
160 }
161