1 /****************************************************************************
2 **
3 ** Copyright (C) 2008-2010 Andrey Rijov <ANDron142@yandex.ru>
4 ** Copyright (C) 2016 Tobias Gläßer
5 **
6 ** This file is part of AQEMU.
7 **
8 ** This program is free software; you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation; either version 2 of the License.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ** Boston, MA  02110-1301, USA.
21 **
22 ****************************************************************************/
23 
24 #include <QFileInfo>
25 #include <QMessageBox>
26 #include <QMenu>
27 #include <QFileDialog>
28 
29 #include "Folder_Sharing_Widget.h"
30 #include "Add_New_Device_Window.h"
31 #include "Utils.h"
32 #include "Create_HDD_Image_Window.h"
33 #include "System_Info.h"
34 #include "Device_Manager_Widget.h"
35 
Folder_Sharing_Widget(QWidget * parent)36 Folder_Sharing_Widget::Folder_Sharing_Widget( QWidget *parent )
37 	: QWidget( parent )
38 {
39 	ui.setupUi( this );
40 
41 	Enabled = true;
42 
43 	//pw = new Properties_Window( this );
44 	Context_Menu = new QMenu( ui.Folders_List );
45 
46 	ui.Folders_List->setSpacing( 3 );
47 	ui.Folders_List->setFlow( QListView::TopToBottom );
48 	ui.Folders_List->setViewMode( QListView::ListMode );
49 
50     connect(this,SIGNAL(Folder_Changed()),this,SLOT(Update_Icons()));
51 }
52 
on_actionAdd_Samba_Folder_triggered()53 void Folder_Sharing_Widget::on_actionAdd_Samba_Folder_triggered()
54 {
55     QString message = tr(R"(To set up a shared SAMBA folder these settings need to be made:
56 
57 <> A SAMBA server (smbd) must be installed on the host
58 <> Network support must be enabled
59 <> The network must be set to 'user mode stack'/'user' mode
60 <> Set the shared folder in Network -> TFTP/SAMBA
61 <> Access the folder in the guest at:
62      \\10.0.2.4\qemu (Windows)
63      mount -t cifs //10.0.2.4/qemu /mnt/path/ (Linux)
64 )");
65 
66     QMessageBox::information( this, tr("How To Setup a Shared SAMBA Folder"), message );
67 }
68 
syncLayout(Device_Manager_Widget * dm)69 void Folder_Sharing_Widget::syncLayout(Device_Manager_Widget* dm)
70 {
71     int w = dm->ui.add_layout_widget->sizeHint().width();
72     ui.add_layout_widget->setMinimumWidth(w);
73     ui.add_layout_widget->setMaximumWidth(w);
74 
75     w = dm->ui.manage_layout_widget->sizeHint().width();
76     ui.manage_layout_widget->setMinimumWidth(w);
77     ui.manage_layout_widget->setMaximumWidth(w);
78 
79     w = dm->ui.view_layout_widget->sizeHint().width();
80     ui.view_layout_widget->setMinimumWidth(w);
81     ui.view_layout_widget->setMaximumWidth(w);
82 }
83 
~Folder_Sharing_Widget()84 Folder_Sharing_Widget::~Folder_Sharing_Widget()
85 {
86 	//if( pw != NULL ) delete pw;
87 	if( Context_Menu != NULL ) delete Context_Menu;
88 }
89 
Set_VM(const Virtual_Machine & vm)90 void Folder_Sharing_Widget::Set_VM( const Virtual_Machine &vm )
91 {
92 	ui.Folders_List->clear();
93 	ui.Label_Connected_To->setText( "" );
94 
95 	ui.TB_Add_Folder->setEnabled( true );
96 
97 	Shared_Folders.clear();
98 
99 	for( int ix = 0; ix < vm.Get_Shared_Folders_List().count(); ++ix )
100 	{
101 		Shared_Folders << vm.Get_Shared_Folders_List()[ix];
102 	}
103 
104 	Update_Icons();
105 	Update_Enabled_Actions();
106 }
107 
Set_Enabled(bool on)108 void Folder_Sharing_Widget::Set_Enabled( bool on )
109 {
110 	Enabled = on;
111 
112 	ui.Label_Add_Folders->setEnabled( on );
113 	ui.TB_Add_Folder->setEnabled( on );
114     ui.TB_Add_Samba_Folder->setEnabled( on );
115 
116 	ui.Label_Manage_Folders->setEnabled( on );
117 	//ui.TB_Edit_Folder->setEnabled( on );
118 	ui.TB_Remove_Folder->setEnabled( on );
119 
120 	ui.Label_View_Mode->setEnabled( on );
121 	ui.TB_Icon_Mode->setEnabled( on );
122 	ui.TB_List_Mode->setEnabled( on );
123 
124 	//ui.Label_Folders_List->setEnabled( on );
125 	ui.Label_Information->setEnabled( on );
126 	ui.Label_Connected_To->setEnabled( true );
127 }
128 
Update_Enabled_Actions()129 void Folder_Sharing_Widget::Update_Enabled_Actions()
130 {
131 	// Adds
132 
133 	ui.actionAdd_Folder->setEnabled( true );
134 	ui.TB_Add_Folder->setEnabled( true );
135 
136 	// Update Information
137 	if( ui.Folders_List->currentItem() != NULL )
138 	{
139 			bool found = false;
140 
141 			for( int fx = 0; fx < 32; ++fx )
142 			{
143 				if( ui.Folders_List->currentItem()->data(512).toString() == "folder" + QString::number(fx) )
144 				{
145 					found = true;
146 
147 					ui.Label_Connected_To->setText( "# "+tr("The 9p filesystem module must be available on the guest")+"\nmkdir /tmp/shared"+QString::number(fx)+"; mount -t 9p -o trans=virtio shared"+QString::number(fx)+" /tmp/shared"+QString::number(fx)+" \\\n                          -o version=9p2000.L,posixacl,cache=mmap" );
148 
149 					//ui.TB_Edit_Folder->setEnabled( true );
150 					ui.actionProperties->setEnabled( true );
151 
152 					ui.TB_Remove_Folder->setEnabled( true );
153 					ui.actionRemove->setEnabled( true );
154 
155 				}
156 			}
157 
158 			if( ! found )
159 			{
160 				//ui.TB_Edit_Folder->setEnabled( false );
161 				ui.actionProperties->setEnabled( false);
162 
163 				ui.TB_Remove_Folder->setEnabled( false );
164 				ui.actionRemove->setEnabled( false );
165 
166 			}
167 	}
168 	else
169 	{
170 		//ui.TB_Edit_Folder->setEnabled( false );
171 		ui.actionProperties->setEnabled( false);
172 
173 		ui.TB_Remove_Folder->setEnabled( false );
174 		ui.actionRemove->setEnabled( false );
175 
176 	}
177 
178 	// Disable widgets
179 	if( ! Enabled )
180 	{
181 		ui.actionAdd_Folder->setEnabled( false );
182 		ui.TB_Add_Folder->setEnabled( false );
183 
184 		ui.TB_Remove_Folder->setEnabled( false );
185 		ui.actionRemove->setEnabled( false );
186 	}
187 }
188 
Update_List_Mode()189 void Folder_Sharing_Widget::Update_List_Mode()
190 {
191 	if( ui.Folders_List->viewMode() == QListView::IconMode )
192 	{
193 		ui.Folders_List->setSpacing( 10 );
194 		ui.Folders_List->setFlow( QListView::LeftToRight );
195 		ui.Folders_List->setViewMode( QListView::IconMode );
196 	}
197 	else
198 	{
199 		ui.Folders_List->setSpacing( 3 );
200 		ui.Folders_List->setFlow( QListView::TopToBottom );
201 		ui.Folders_List->setViewMode( QListView::ListMode );
202 	}
203 }
204 
on_Folders_List_customContextMenuRequested(const QPoint & pos)205 void Folder_Sharing_Widget::on_Folders_List_customContextMenuRequested( const QPoint &pos )
206 {
207 	QListWidgetItem *it = ui.Folders_List->itemAt( pos );
208 
209     /* //TODO
210 	if( it != NULL )
211 	{
212 		{
213 			bool found = false;
214 
215 			for( int fx = 0; fx < 32; ++fx )
216 			{
217 				if( ui.Folders_List->currentItem()->data(512).toString() == "folder" + QString::number(fx) )
218 				{
219 					found = true;
220 
221 					Context_Menu = new QMenu( ui.Folders_List );
222 
223 					//Context_Menu->addAction( ui.actionProperties );
224 					Context_Menu->addAction( ui.actionRemove );
225 
226 					Context_Menu->exec( ui.Folders_List->mapToGlobal(pos) );
227 				}
228 			}
229 
230 			if( ! found )
231 			{
232 				AQError( "void Folder_Sharing_Widget::on_Folders_List_customContextMenuRequested( const QPoint &pos )",
233 						 "Incorrect folder!" );
234 			}
235 		}
236 	}
237 	else
238 	{
239 		Context_Menu = new QMenu( ui.Folders_List );
240 
241 		Context_Menu->addAction( ui.actionAdd_Folder );
242 		Context_Menu->addSeparator();
243 		Context_Menu->addAction( ui.actionIcon_Mode );
244 		Context_Menu->addAction( ui.actionList_Mode );
245 
246 		Context_Menu->exec( ui.Folders_List->mapToGlobal(pos) );
247 	}
248     */
249 }
250 
on_Folders_List_currentItemChanged(QListWidgetItem * current,QListWidgetItem * previous)251 void Folder_Sharing_Widget::on_Folders_List_currentItemChanged(
252 				QListWidgetItem *current, QListWidgetItem *previous )
253 {
254 	Update_Enabled_Actions();
255 }
256 
on_Folders_List_itemDoubleClicked(QListWidgetItem * item)257 void Folder_Sharing_Widget::on_Folders_List_itemDoubleClicked( QListWidgetItem *item )
258 {
259 	on_actionProperties_triggered();
260 }
261 
on_actionAdd_Folder_triggered()262 void Folder_Sharing_Widget::on_actionAdd_Folder_triggered()
263 {
264     QString path = QFileDialog::getExistingDirectory(this, "Select to folder to be shared");
265 
266     if ( ! path.isEmpty() )
267     {
268 		Shared_Folders << VM_Shared_Folder(true,path);
269 
270 		emit Folder_Changed();
271     }
272 }
273 
on_actionProperties_triggered()274 void Folder_Sharing_Widget::on_actionProperties_triggered()
275 {
276  /* stub
277  */
278 }
279 
on_actionRemove_triggered()280 void Folder_Sharing_Widget::on_actionRemove_triggered()
281 {
282 	int mes_ret = QMessageBox::question( this, tr("Remove?"),
283 			tr("Remove Folder?"),
284 			QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
285 
286 	if( mes_ret == QMessageBox::No ) return;
287 
288     bool found = false;
289 	for( int fx = 0; fx < 32; ++fx )
290 	{
291 		if( ui.Folders_List->currentItem()->data(512).toString() == "folder" + QString::number(fx) )
292 		{
293 			found = true;
294 
295 			Shared_Folders.removeAt( fx );
296 		}
297 	}
298 
299 	if( ! found )
300 	{
301 		AQError( "void Folder_Sharing_Widget::on_actionRemove_triggered()",
302 				 "Incorrect folder!" );
303 		return;
304 	}
305 
306 	emit Folder_Changed();
307 }
308 
on_actionIcon_Mode_triggered()309 void Folder_Sharing_Widget::on_actionIcon_Mode_triggered()
310 {
311 	ui.Folders_List->setSpacing( 10 );
312 	ui.Folders_List->setFlow( QListView::LeftToRight );
313 	ui.Folders_List->setViewMode( QListView::IconMode );
314 }
315 
on_actionList_Mode_triggered()316 void Folder_Sharing_Widget::on_actionList_Mode_triggered()
317 {
318 	ui.Folders_List->setSpacing( 3 );
319 	ui.Folders_List->setFlow( QListView::TopToBottom );
320 	ui.Folders_List->setViewMode( QListView::ListMode );
321 }
322 
Update_Icons()323 void Folder_Sharing_Widget::Update_Icons()
324 {
325 	ui.Folders_List->clear();
326 
327 	for( int ix = 0; ix < Shared_Folders.count(); ++ix )
328 	{
329 		QListWidgetItem *hdit = new QListWidgetItem( QIcon(":/open-folder.png"),
330 													 Shared_Folders[ix].Get_Folder(), ui.Folders_List );
331 		hdit->setData( 512, "folder" + QString::number(ix) );
332 
333 		ui.Folders_List->addItem( hdit );
334 	}
335 }
336 
337