VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/ui/VBoxSharedFoldersSettings.ui.h@ 1726

Last change on this file since 1726 was 1062, checked in by vboxsync, 18 years ago

FE/Qt: Added proper Shared Folders and Host IFace icons.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.1 KB
Line 
1/**
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * "Shared Folders" settings dialog UI include (Qt Designer)
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23/****************************************************************************
24** ui.h extension file, included from the uic-generated form implementation.
25**
26** If you want to add, delete, or rename functions or slots, use
27** Qt Designer to update this file, preserving your code.
28**
29** You should not define a constructor or destructor in this file.
30** Instead, write your code in functions called init() and destroy().
31** These will automatically be called by the form's constructor and
32** destructor.
33*****************************************************************************/
34
35
36class VBoxRichListItem : public QListViewItem
37{
38public:
39
40 enum { QIRichListItemId = 1010 };
41
42 enum FormatType
43 {
44 IncorrectFormat = 0,
45 EllipsisStart = 1,
46 EllipsisMiddle = 2,
47 EllipsisEnd = 3,
48 EllipsisFile = 4
49 };
50
51 VBoxRichListItem (FormatType aFormat, QListView *aParent,
52 const QString& aName, const QString& aNull,
53 const QString& aKey) :
54 QListViewItem (aParent, aName, aNull, aKey), mFormat (aFormat)
55 {
56 }
57
58 VBoxRichListItem (FormatType aFormat, QListViewItem *aParent,
59 const QString& aName, const QString& aPath) :
60 QListViewItem (aParent, aName, aPath), mFormat (aFormat)
61 {
62 mTextList << aName << aPath;
63 }
64
65 int rtti() const { return QIRichListItemId; }
66
67 QString getText (int aIndex)
68 {
69 return aIndex >= 0 && aIndex < (int)mTextList.size() ?
70 mTextList [aIndex] : QString::null;
71 }
72
73 void updateText (int aColumn, const QString &aText)
74 {
75 if (aColumn >= 0 && aColumn < (int)mTextList.size())
76 mTextList [aColumn] = aText;
77 }
78
79protected:
80
81 void paintCell (QPainter *aPainter, const QColorGroup &aColorGroup,
82 int aColumn, int aWidth, int aAlign)
83 {
84 processColumn (aColumn, aWidth);
85 QListViewItem::paintCell (aPainter, aColorGroup, aColumn, aWidth, aAlign);
86 }
87
88 void processColumn (int aColumn, int aWidth)
89 {
90 QString oneString = aColumn >= 0 && aColumn < (int)mTextList.size() ?
91 mTextList [aColumn] : QString::null;
92 if (oneString.isNull())
93 return;
94 int oldSize = listView()->fontMetrics().width (oneString);
95 int indentSize = listView()->fontMetrics().width ("...x");
96
97 /* compress text */
98 int start = 0;
99 int finish = 0;
100 int position = 0;
101 int textWidth = 0;
102 do {
103 textWidth = listView()->fontMetrics().width (oneString);
104 if (textWidth + indentSize > aWidth)
105 {
106 start = 0;
107 finish = oneString.length();
108
109 /* selecting remove position */
110 switch (mFormat)
111 {
112 case EllipsisStart:
113 position = start;
114 break;
115 case EllipsisMiddle:
116 position = (finish - start) / 2;
117 break;
118 case EllipsisEnd:
119 position = finish - 1;
120 break;
121 case EllipsisFile:
122 {
123 QRegExp regExp ("([\\\\/][^\\\\^/]+[\\\\/]?$)");
124 int newFinish = regExp.search (oneString);
125 if (newFinish != -1)
126 finish = newFinish;
127 position = (finish - start) / 2;
128 break;
129 }
130 default:
131 AssertMsgFailed (("Invalid format type\n"));
132 }
133
134 if (position == finish)
135 break;
136 oneString.remove (position, 1);
137 }
138 } while (textWidth + indentSize > aWidth);
139 if (position || mFormat == EllipsisFile) oneString.insert (position, "...");
140
141 int newSize = listView()->fontMetrics().width (oneString);
142 setText (aColumn, newSize < oldSize ? oneString : mTextList [aColumn]);
143 }
144
145 FormatType mFormat;
146 QStringList mTextList;
147};
148
149
150class VBoxAddSFDialog : public QDialog
151{
152 Q_OBJECT
153
154public:
155
156 enum DialogType { AddDType, EditDType };
157
158 VBoxAddSFDialog (QWidget *aParent, VBoxAddSFDialog::DialogType aType) :
159 QDialog (aParent, "VBoxAddSFDialog", true /* modal */),
160 mLePath (0), mLeName (0)
161 {
162 switch (aType)
163 {
164 case AddDType:
165 setCaption (tr ("Add Share"));
166 break;
167 case EditDType:
168 setCaption (tr ("Edit Share"));
169 break;
170 default:
171 AssertMsgFailed (("Incorrect SF Dialog type\n"));
172 }
173 QVBoxLayout *mainLayout = new QVBoxLayout (this, 10, 10, "mainLayout");
174
175 /* Setup Input layout */
176 QGridLayout *inputLayout = new QGridLayout (mainLayout, 2, 3, 10, "inputLayout");
177 QLabel *lbPath = new QLabel ("Folder Path", this);
178 mLePath = new QLineEdit (this);
179 QToolButton *tbPath = new QToolButton (this);
180 QLabel *lbName = new QLabel ("Folder Name", this);
181 mLeName = new QLineEdit (this);
182 tbPath->setIconSet (VBoxGlobal::iconSet ("select_file_16px.png",
183 "select_file_dis_16px.png"));
184 tbPath->setFocusPolicy (QWidget::TabFocus);
185 connect (mLePath, SIGNAL (textChanged (const QString &)),
186 this, SLOT (validate()));
187 connect (mLeName, SIGNAL (textChanged (const QString &)),
188 this, SLOT (validate()));
189 connect (tbPath, SIGNAL (clicked()), this, SLOT (showFileDialog()));
190 QWhatsThis::add (mLePath, tr ("Enter existing path for the shared folder here"));
191 QWhatsThis::add (mLeName, tr ("Enter name for the shared folder to be created"));
192 QWhatsThis::add (tbPath, tr ("Click to invoke <open folder> dialog"));
193
194 inputLayout->addWidget (lbPath, 0, 0);
195 inputLayout->addWidget (mLePath, 0, 1);
196 inputLayout->addWidget (tbPath, 0, 2);
197 inputLayout->addWidget (lbName, 1, 0);
198 inputLayout->addMultiCellWidget (mLeName, 1, 1, 1, 2);
199
200 /* Setup Button layout */
201 QHBoxLayout *buttonLayout = new QHBoxLayout (mainLayout, 10, "buttonLayout");
202 mBtOk = new QPushButton ("OK", this, "btOk");
203 QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
204 QPushButton *btCancel = new QPushButton ("Cancel", this, "btCancel");
205 connect (mBtOk, SIGNAL (clicked()), this, SLOT (accept()));
206 connect (btCancel, SIGNAL (clicked()), this, SLOT (reject()));
207
208 buttonLayout->addWidget (mBtOk);
209 buttonLayout->addItem (spacer);
210 buttonLayout->addWidget (btCancel);
211
212 /* Validate fields */
213 validate();
214 }
215
216 ~VBoxAddSFDialog() {}
217
218 QString getPath() { return mLePath->text(); }
219 QString getName() { return mLeName->text(); }
220
221 void setPath (const QString &aPath) { mLePath->setText (aPath); }
222 void setName (const QString &aName) { mLeName->setText (aName); }
223
224private slots:
225
226 void validate()
227 {
228 mBtOk->setEnabled (!mLePath->text().isEmpty() && !mLeName->text().isEmpty());
229 }
230
231 void showFileDialog()
232 {
233 QFileDialog dlg (QDir::rootDirPath(), QString::null, this);
234 dlg.setMode (QFileDialog::DirectoryOnly);
235 dlg.setCaption (tr ("Select a folder to share"));
236 if (dlg.exec() == QDialog::Accepted)
237 {
238 QString folderName = QDir::convertSeparators (dlg.selectedFile());
239 QRegExp commonRule ("[\\\\/]([^\\\\^/]+)[\\\\/]?$");
240 QRegExp rootRule ("(([a-zA-Z])[^\\\\^/])?[\\\\/]$");
241 if (commonRule.search (folderName) != -1)
242 {
243 /* processing non-root folder */
244 mLePath->setText (folderName.remove (QRegExp ("[\\\\/]$")));
245 mLeName->setText (commonRule.cap (1));
246 }
247 else if (rootRule.search (folderName) != -1)
248 {
249 /* processing root folder */
250 mLePath->setText (folderName);
251#if defined(Q_WS_WIN32)
252 mLeName->setText (rootRule.cap (2) + "_DRIVE");
253#elif defined(Q_WS_X11)
254 mLeName->setText ("ROOT");
255#endif
256 }
257 else
258 return; /* hm, what type of folder it was? */
259 }
260 }
261
262private:
263
264 void showEvent (QShowEvent *aEvent)
265 {
266 setFixedHeight (height());
267 QDialog::showEvent (aEvent);
268 }
269
270 QPushButton *mBtOk;
271 QLineEdit *mLePath;
272 QLineEdit *mLeName;
273};
274
275
276void VBoxSharedFoldersSettings::init()
277{
278 mDialogType = WrongType;
279 new QIListViewSelectionPreserver (this, listView);
280 listView->setShowToolTips (false);
281 listView->setRootIsDecorated (true);
282 tbAdd->setIconSet (VBoxGlobal::iconSet ("add_shared_folder_16px.png",
283 "add_shared_folder_disabled_16px.png"));
284 tbEdit->setIconSet (VBoxGlobal::iconSet ("edit_shared_folder_16px.png",
285 "edit_shared_folder_disabled_16px.png"));
286 tbRemove->setIconSet (VBoxGlobal::iconSet ("revome_shared_folder_16px.png",
287 "revome_shared_folder_disabled_16px.png"));
288 QToolTip::add (tbAdd, tr ("Add a new shared folder"));
289 QToolTip::add (tbEdit, tr ("Edit the selected shared folder"));
290 QToolTip::add (tbRemove, tr ("Remove the selected shared folder"));
291 connect (tbAdd, SIGNAL (clicked()), this, SLOT (tbAddPressed()));
292 connect (tbEdit, SIGNAL (clicked()), this, SLOT (tbEditPressed()));
293 connect (tbRemove, SIGNAL (clicked()), this, SLOT (tbRemovePressed()));
294 connect (listView, SIGNAL (currentChanged (QListViewItem *)),
295 this, SLOT (processCurrentChanged (QListViewItem *)));
296 connect (listView, SIGNAL (onItem (QListViewItem *)),
297 this, SLOT (processOnItem (QListViewItem *)));
298
299 mIsListViewChanged = false;
300}
301
302void VBoxSharedFoldersSettings::setDialogType (
303 VBoxSharedFoldersSettings::SFDialogType aType)
304{
305 mDialogType = aType;
306}
307
308
309void VBoxSharedFoldersSettings::removeSharedFolder (const QString & aName,
310 const QString & aPath,
311 SFDialogType aType)
312{
313 switch (aType)
314 {
315 case GlobalType:
316 {
317 /* This feature is not implemented yet */
318 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
319 break;
320 }
321 case MachineType:
322 {
323 Assert (!mMachine.isNull());
324 mMachine.RemoveSharedFolder (aName);
325 if (!mMachine.isOk())
326 vboxProblem().cannotRemoveSharedFolder (this, mMachine,
327 aName, aPath);
328 break;
329 }
330 case ConsoleType:
331 {
332 Assert (!mConsole.isNull());
333 mConsole.RemoveSharedFolder (aName);
334 if (!mConsole.isOk())
335 vboxProblem().cannotRemoveSharedFolder (this, mConsole,
336 aName, aPath);
337 break;
338 }
339 default:
340 {
341 AssertMsgFailed (("Incorrect shared folder type\n"));
342 }
343 }
344}
345
346void VBoxSharedFoldersSettings::createSharedFolder (const QString & aName,
347 const QString & aPath,
348 SFDialogType aType)
349{
350 switch (aType)
351 {
352 case GlobalType:
353 {
354 /* This feature is not implemented yet */
355 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
356 break;
357 }
358 case MachineType:
359 {
360 Assert (!mMachine.isNull());
361 mMachine.CreateSharedFolder (aName, aPath);
362 if (!mMachine.isOk())
363 vboxProblem().cannotCreateSharedFolder (this, mMachine,
364 aName, aPath);
365 break;
366 }
367 case ConsoleType:
368 {
369 Assert (!mConsole.isNull());
370 mConsole.CreateSharedFolder (aName, aPath);
371 if (!mConsole.isOk())
372 vboxProblem().cannotCreateSharedFolder (this, mConsole,
373 aName, aPath);
374 break;
375 }
376 default:
377 {
378 AssertMsgFailed (("Incorrect shared folder type\n"));
379 }
380 }
381}
382
383
384void VBoxSharedFoldersSettings::getFromGlobal()
385{
386 /* This feature is not implemented yet */
387 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
388
389 /*
390 QString name = tr (" Global Folders");
391 QString key (QString::number (GlobalType));
392 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
393 listView, name, QString::null, key);
394 getFrom (vboxGlobal().virtualBox().GetSharedFolders().Enumerate(), root);
395 */
396}
397
398void VBoxSharedFoldersSettings::getFromMachine (const CMachine &aMachine)
399{
400 mMachine = aMachine;
401 QString name = tr (" Machine Folders");
402 QString key (QString::number (MachineType));
403 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
404 listView, name, QString::null, key);
405 getFrom (mMachine.GetSharedFolders().Enumerate(), root);
406}
407
408void VBoxSharedFoldersSettings::getFromConsole (const CConsole &aConsole)
409{
410 mConsole = aConsole;
411 QString name = tr (" Transient Folders");
412 QString key (QString::number (ConsoleType));
413 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
414 listView, name, QString::null, key);
415 getFrom (mConsole.GetSharedFolders().Enumerate(), root);
416}
417
418void VBoxSharedFoldersSettings::getFrom (const CSharedFolderEnumerator &aEn,
419 QListViewItem *aRoot)
420{
421 aRoot->setSelectable (false);
422 while (aEn.HasMore())
423 {
424 CSharedFolder sf = aEn.GetNext();
425 new VBoxRichListItem (VBoxRichListItem::EllipsisFile, aRoot,
426 sf.GetName(), sf.GetHostPath());
427 }
428 listView->setOpen (aRoot, true);
429 listView->setCurrentItem (aRoot);
430 processCurrentChanged (aRoot);
431}
432
433
434void VBoxSharedFoldersSettings::putBackToGlobal()
435{
436 /* This feature is not implemented yet */
437 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
438
439 /*
440 if (!mIsListViewChanged) return;
441 // This function is only available for GlobalType dialog
442 Assert (mDialogType == GlobalType);
443 // Searching for GlobalType item's root
444 QListViewItem *root = listView->findItem (QString::number (GlobalType), 2);
445 Assert (root);
446 CSharedFolderEnumerator en = vboxGlobal().virtualBox().GetSharedFolders().Enumerate();
447 putBackTo (en, root);
448 */
449}
450
451void VBoxSharedFoldersSettings::putBackToMachine()
452{
453 if (!mIsListViewChanged)
454 return;
455
456 /* This function is only available for MachineType dialog */
457 Assert (mDialogType == MachineType);
458 /* Searching for MachineType item's root */
459 QListViewItem *root = listView->findItem (QString::number (MachineType), 2);
460 Assert (root);
461 CSharedFolderEnumerator en = mMachine.GetSharedFolders().Enumerate();
462 putBackTo (en, root);
463}
464
465void VBoxSharedFoldersSettings::putBackToConsole()
466{
467 if (!mIsListViewChanged)
468 return;
469
470 /* This function is only available for ConsoleType dialog */
471 Assert (mDialogType == ConsoleType);
472 /* Searching for ConsoleType item's root */
473 QListViewItem *root = listView->findItem (QString::number (ConsoleType), 2);
474 Assert (root);
475 CSharedFolderEnumerator en = mConsole.GetSharedFolders().Enumerate();
476 putBackTo (en, root);
477}
478
479void VBoxSharedFoldersSettings::putBackTo (CSharedFolderEnumerator &aEn,
480 QListViewItem *aRoot)
481{
482 Assert (!aRoot->text (2).isNull());
483 SFDialogType type = (SFDialogType)aRoot->text (2).toInt();
484
485 /* deleting all existing folders if the list */
486 while (aEn.HasMore())
487 {
488 CSharedFolder sf = aEn.GetNext();
489 removeSharedFolder (sf.GetName(), sf.GetHostPath(), type);
490 }
491
492 /* saving all machine related list view items */
493 QListViewItem *iterator = aRoot->firstChild();
494 while (iterator)
495 {
496 VBoxRichListItem *item = 0;
497 if (iterator->rtti() == VBoxRichListItem::QIRichListItemId)
498 item = static_cast<VBoxRichListItem*> (iterator);
499 if (item && !item->getText (0).isNull() && !item->getText (1).isNull())
500 createSharedFolder (item->getText (0), item->getText (1), type);
501 else
502 AssertMsgFailed (("Incorrect listview item type\n"));
503 iterator = iterator->nextSibling();
504 }
505}
506
507
508void VBoxSharedFoldersSettings::tbAddPressed()
509{
510 /* Invoke Add-Box Dialog */
511 VBoxAddSFDialog dlg (this, VBoxAddSFDialog::AddDType);
512 if (dlg.exec() != QDialog::Accepted)
513 return;
514 QString name = dlg.getName();
515 QString path = dlg.getPath();
516 /* Shared folder's name & path could not be empty */
517 Assert (!name.isEmpty() && !path.isEmpty());
518 /* Searching root for the new listview item */
519 QListViewItem *root = listView->findItem (QString::number (mDialogType), 2);
520 Assert (root);
521 /* Appending a new listview item to the root */
522 VBoxRichListItem *item = new VBoxRichListItem (VBoxRichListItem::EllipsisFile,
523 root, name, path);
524 listView->ensureItemVisible (item);
525 listView->setCurrentItem (item);
526 processCurrentChanged (item);
527 listView->setFocus();
528 mIsListViewChanged = true;
529}
530
531void VBoxSharedFoldersSettings::tbEditPressed()
532{
533 /* Check selected item */
534 QListViewItem *selectedItem = listView->selectedItem();
535 VBoxRichListItem *item = 0;
536 if (selectedItem->rtti() == VBoxRichListItem::QIRichListItemId)
537 item = static_cast<VBoxRichListItem*> (selectedItem);
538 Assert (item);
539 /* Invoke Add-Box Dialog */
540 VBoxAddSFDialog dlg (this, VBoxAddSFDialog::EditDType);
541 dlg.setPath (item->getText (1));
542 dlg.setName (item->getText (0));
543 if (dlg.exec() != QDialog::Accepted)
544 return;
545 QString name = dlg.getName();
546 QString path = dlg.getPath();
547 /* Shared folder's name & path could not be empty */
548 Assert (!name.isEmpty() && !path.isEmpty());
549 /* Updating an edited listview item */
550 item->updateText (1, path);
551 item->updateText (0, name);
552 mIsListViewChanged = true;
553}
554
555void VBoxSharedFoldersSettings::tbRemovePressed()
556{
557 Assert (listView->selectedItem());
558 delete listView->selectedItem();
559 mIsListViewChanged = true;
560}
561
562
563void VBoxSharedFoldersSettings::processOnItem (QListViewItem *aItem)
564{
565 VBoxRichListItem *item = 0;
566 if (aItem->rtti() == VBoxRichListItem::QIRichListItemId)
567 item = static_cast<VBoxRichListItem*> (aItem);
568 Assert (item);
569 QString tip = tr ("<nobr>Name:&nbsp;&nbsp;%1</nobr><br>"
570 "<nobr>Path:&nbsp;&nbsp;%2</nobr>")
571 .arg (item->getText (0)).arg (item->getText (1));
572 if (!item->getText (0).isNull() && !item->getText (1).isNull())
573 QToolTip::add (listView->viewport(), listView->itemRect (aItem), tip);
574 else
575 QToolTip::remove (listView->viewport());
576}
577
578void VBoxSharedFoldersSettings::processCurrentChanged (QListViewItem *aItem)
579{
580 if (aItem && aItem->isSelectable() && listView->selectedItem() != aItem)
581 listView->setSelected (aItem, true);
582 bool addEnabled = aItem &&
583 (isEditable (aItem->text (2)) ||
584 aItem->parent() && isEditable (aItem->parent()->text (2)));
585 bool removeEnabled = aItem && aItem->parent() &&
586 isEditable (aItem->parent()->text (2));
587 tbAdd->setEnabled (addEnabled);
588 tbEdit->setEnabled (removeEnabled);
589 tbRemove->setEnabled (removeEnabled);
590}
591
592bool VBoxSharedFoldersSettings::isEditable (const QString &aKey)
593{
594 /* mDialogType should be sutup already */
595 Assert (mDialogType);
596 /* simple item has no key information */
597 if (aKey.isEmpty())
598 return false;
599 SFDialogType type = (SFDialogType)aKey.toInt();
600 if (!type)
601 AssertMsgFailed (("Incorrect listview item key value\n"));
602 return type == mDialogType;
603}
604
605
606#include "VBoxSharedFoldersSettings.ui.moc"
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette