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 
18 #include "folderbutton.h"
19 #include "x2goclientconfig.h"
20 #include "x2goutils.h"
21 
22 #include <QFont>
23 #include <QPixmap>
24 #include <QLabel>
25 #include "x2gosettings.h"
26 #include <QDir>
27 #include <QLayout>
28 #include <QPushButton>
29 #include "onmainwindow.h"
30 #include "x2gologdebug.h"
31 #include <QApplication>
32 #include <QDesktopWidget>
33 #include "sessionexplorer.h"
34 #include <QMouseEvent>
35 
36 
FolderButton(ONMainWindow * mw,QWidget * parent,QString folderPath,QString folderName)37 FolderButton::FolderButton ( ONMainWindow* mw,QWidget *parent, QString folderPath, QString folderName )
38     : SVGFrame ( ":/img/svg/folder.svg",false,parent )
39 {
40     QPalette pal=palette();
41     pal.setColor ( QPalette::Active, QPalette::WindowText, QPalette::Mid );
42     pal.setColor ( QPalette::Active, QPalette::ButtonText, QPalette::Mid );
43     pal.setColor ( QPalette::Active, QPalette::Text, QPalette::Mid );
44     pal.setColor ( QPalette::Inactive, QPalette::WindowText, QPalette::Mid );
45     pal.setColor ( QPalette::Inactive, QPalette::ButtonText, QPalette::Mid );
46     pal.setColor ( QPalette::Inactive, QPalette::Text, QPalette::Mid );
47 
48     setPalette(pal);
49 
50     path=folderPath;
51     name=folderName;
52 
53     QFont fnt=font();
54     if ( mw->retMiniMode() )
55 #ifdef Q_WS_HILDON
56         fnt.setPointSize ( 10 );
57 #else
58         fnt.setPointSize ( 9 );
59 #endif
60     setFont ( fnt );
61     setFocusPolicy ( Qt::NoFocus );
62     bool miniMode=mw->retMiniMode();
63     if ( !miniMode )
64         setFixedSize ( 340,190 );
65     else
66         setFixedSize ( 250,145 );
67 
68     par= mw;
69     connect ( this,SIGNAL ( clicked() ),this,SLOT ( slotClicked() ) );
70 
71 
72     nameLabel=new QLabel ( this );
73     description=tr("Sessions folder");
74 
75     setChildrenList(QStringList() );
76 
77     icon=new QLabel ( this );
78 
79     nameLabel->setWordWrap(true);
80     nameLabel->setTextInteractionFlags(Qt::NoTextInteraction);
81 
82     icon->move ( 10,25 );
83 
84     if ( !miniMode )
85     {
86         nameLabel->move ( 80,34 );
87         nameLabel->setFixedSize(235,135);
88     }
89     else
90     {
91         nameLabel->move ( 64,18 );
92         nameLabel->setFixedSize(170,120);
93     }
94     loadIcon();
95 }
96 
~FolderButton()97 FolderButton::~FolderButton()
98 {}
99 
loadIcon()100 void FolderButton::loadIcon()
101 {
102     X2goSettings *st;
103 
104     if (par->getBrokerMode())
105         st=new X2goSettings(par->getConfig()->iniFile,QSettings::IniFormat);
106     else
107         st= new X2goSettings( "sessions" );
108 
109     QString sessIcon=":/img/icons/128x128/folder.png";
110     QPixmap* pix;
111 
112     QString normPath=(path+"/"+name).split("/",QString::SkipEmptyParts).join("::");
113 
114     QByteArray picture = QByteArray::fromBase64( st->setting()->value ( "icon_"+normPath,
115                        ( QVariant )QString()).toString().toLocal8Bit());
116     if(!picture.size())
117     {
118         pix=new QPixmap( sessIcon );
119     }
120     else
121     {
122         pix=new QPixmap();
123         pix->loadFromData(picture);
124     }
125     bool miniMode=par->retMiniMode();
126 
127     if ( !miniMode )
128     {
129         icon->setPixmap ( pix->scaled ( 64,64,Qt::IgnoreAspectRatio,
130                                         Qt::SmoothTransformation ) );
131     }
132     else
133     {
134         icon->setPixmap ( pix->scaled ( 48,48,Qt::IgnoreAspectRatio,
135                                         Qt::SmoothTransformation ) );
136     }
137 
138     delete pix;
139 }
140 
slotClicked()141 void FolderButton::slotClicked()
142 {
143     emit folderSelected ( this );
144 }
145 
lessThen(const FolderButton * b1,const FolderButton * b2)146 bool FolderButton::lessThen ( const FolderButton* b1,
147                               const FolderButton* b2 )
148 {
149     return b1->name.toLower().localeAwareCompare (
150                b2->name.toLower() ) <0;
151 }
152 
mousePressEvent(QMouseEvent * event)153 void FolderButton::mousePressEvent ( QMouseEvent * event )
154 {
155     SVGFrame::mousePressEvent ( event );
156     loadBg ( ":/img/svg/folder_grey.svg" );
157 }
158 
mouseReleaseEvent(QMouseEvent * event)159 void FolderButton::mouseReleaseEvent ( QMouseEvent * event )
160 {
161     SVGFrame::mouseReleaseEvent ( event );
162     int x=event->x();
163     int y=event->y();
164     loadBg ( ":/img/svg/folder.svg" );
165     if ( x>=0 && x< width() && y>=0 && y<height() )
166         emit clicked();
167 }
168 
setChildrenList(QStringList children)169 void FolderButton::setChildrenList(QStringList children)
170 {
171     QString text="<b>"+name+"</b>";
172     if(description.length()>0)
173     {
174         text+="<br>("+description+")";
175     }
176     if(children.count())
177     {
178         text+="<p style=\"color:grey\">"+children.join(", ")+"</p>";
179     }
180     nameLabel->setText(text);
181 }
182