VirtualBox

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

Last change on this file since 10621 was 9550, checked in by vboxsync, 17 years ago

VirtualBox GUI: No names with spaces or only a " " is allowed for Shared Folders anymore. Needs to be done in VirtualBox4 and VBoxManage as well. Length check??

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.7 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-2007 Sun Microsystems, Inc.
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 (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
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
36typedef QPair<QString, VBoxSharedFoldersSettings::SFDialogType> SFolderName;
37typedef QValueList<SFolderName> SFoldersNameList;
38
39
40class VBoxRichListItem : public QListViewItem
41{
42public:
43
44 enum { QIRichListItemId = 1010 };
45
46 enum FormatType
47 {
48 IncorrectFormat = 0,
49 EllipsisStart = 1,
50 EllipsisMiddle = 2,
51 EllipsisEnd = 3,
52 EllipsisFile = 4
53 };
54
55 VBoxRichListItem (FormatType aFormat, QListView *aParent,
56 const QString& aName, const QString& aNull1,
57 const QString& aNull2, const QString& aKey) :
58 QListViewItem (aParent, aName, aNull1, aNull2, aKey), mFormat (aFormat)
59 {
60 }
61
62 VBoxRichListItem (FormatType aFormat, QListViewItem *aParent,
63 const QString& aName, const QString& aPath,
64 const QString& aAccess, const QString& aEdited) :
65 QListViewItem (aParent, aName, aPath, aAccess, aEdited), mFormat (aFormat)
66 {
67 mTextList << aName << aPath << aAccess << aEdited;
68 }
69
70 int rtti() const { return QIRichListItemId; }
71
72 int compare (QListViewItem *aItem, int aColumn, bool aAscending) const
73 {
74 /* Sorting the children always by name: */
75 if (parent() && aItem->parent())
76 return QListViewItem::compare (aItem, 0, aAscending);
77 /* Sorting the root items always by key: */
78 else if (!parent() && !aItem->parent())
79 return QListViewItem::compare (aItem, 3, aAscending);
80 else
81 return QListViewItem::compare (aItem, aColumn, aAscending);
82 }
83
84 VBoxRichListItem* nextSibling() const
85 {
86 QListViewItem *item = QListViewItem::nextSibling();
87 return item && item->rtti() == QIRichListItemId ?
88 static_cast<VBoxRichListItem*> (item) : 0;
89 }
90
91 QString getText (int aIndex) const
92 {
93 return aIndex >= 0 && aIndex < (int)mTextList.size() ?
94 mTextList [aIndex] : QString::null;
95 }
96
97 void updateText (int aColumn, const QString &aText)
98 {
99 if (aColumn >= 0 && aColumn < (int)mTextList.size())
100 mTextList [aColumn] = aText;
101 }
102
103protected:
104
105 void paintCell (QPainter *aPainter, const QColorGroup &aColorGroup,
106 int aColumn, int aWidth, int aAlign)
107 {
108 if (!parent())
109 {
110 /* Make root items occupy the whole width */
111 aWidth = listView()->viewport()->width();
112
113 if (aColumn > 0)
114 {
115 /* For columns other than the first one, paint the overlapping
116 * portion of the first column after correcting the window */
117 aPainter->save();
118 QRect wnd = aPainter->window();
119 int dx = -listView()->treeStepSize();
120 for (int i = 0; i < aColumn; ++ i)
121 dx += listView()->columnWidth (i);
122 wnd.moveBy (dx, 0);
123 aPainter->setWindow (wnd);
124 QListViewItem::paintCell (aPainter, aColorGroup, 0, aWidth, aAlign);
125 aPainter->restore();
126 return;
127 }
128
129 QListViewItem::paintCell (aPainter, aColorGroup, aColumn, aWidth, aAlign);
130 }
131 else
132 {
133 processColumn (aColumn, aWidth);
134 QListViewItem::paintCell (aPainter, aColorGroup, aColumn, aWidth, aAlign);
135 }
136 }
137
138 int width (const QFontMetrics &aFontMetrics, const QListView *, int aColumn) const
139 {
140 return parent() ?
141 aFontMetrics.boundingRect (getText (aColumn)).width() +
142 aFontMetrics.width ("...x") /* indent size */ : 0;
143 }
144
145 void processColumn (int aColumn, int aWidth)
146 {
147 QString oneString = aColumn >= 0 && aColumn < (int)mTextList.size() ?
148 mTextList [aColumn] : QString::null;
149 if (oneString.isNull())
150 return;
151 int oldSize = listView()->fontMetrics().width (oneString);
152 int indentSize = listView()->fontMetrics().width ("...x");
153
154 /* compress text */
155 int start = 0;
156 int finish = 0;
157 int position = 0;
158 int textWidth = 0;
159 do {
160 textWidth = listView()->fontMetrics().width (oneString);
161 if (textWidth + indentSize > aWidth)
162 {
163 start = 0;
164 finish = oneString.length();
165
166 /* selecting remove position */
167 switch (mFormat)
168 {
169 case EllipsisStart:
170 position = start;
171 break;
172 case EllipsisMiddle:
173 position = (finish - start) / 2;
174 break;
175 case EllipsisEnd:
176 position = finish - 1;
177 break;
178 case EllipsisFile:
179 {
180 QRegExp regExp ("([\\\\/][^\\\\^/]+[\\\\/]?$)");
181 int newFinish = regExp.search (oneString);
182 if (newFinish != -1)
183 finish = newFinish;
184 position = (finish - start) / 2;
185 break;
186 }
187 default:
188 AssertMsgFailed (("Invalid format type\n"));
189 }
190
191 if (position == finish)
192 break;
193 oneString.remove (position, 1);
194 }
195 } while (textWidth + indentSize > aWidth);
196 if (position || mFormat == EllipsisFile) oneString.insert (position, "...");
197
198 int newSize = listView()->fontMetrics().width (oneString);
199 setText (aColumn, newSize < oldSize ? oneString : mTextList [aColumn]);
200 }
201
202 FormatType mFormat;
203 QStringList mTextList;
204};
205
206
207class VBoxAddSFDialog : public QDialog
208{
209 Q_OBJECT
210
211public:
212
213 enum DialogType { AddDialogType, EditDialogType };
214
215 VBoxAddSFDialog (VBoxSharedFoldersSettings *aParent,
216 VBoxAddSFDialog::DialogType aType,
217 bool aEnableSelector /* for "permanent" checkbox */,
218 const SFoldersNameList &aUsedNames)
219 : QDialog (aParent, "VBoxAddSFDialog", true /* modal */)
220 , mLePath (0), mLeName (0), mCbPermanent (0), mCbReadonly (0)
221 , mUsedNames (aUsedNames)
222 {
223 switch (aType)
224 {
225 case AddDialogType:
226 setCaption (tr ("Add Share"));
227 break;
228 case EditDialogType:
229 setCaption (tr ("Edit Share"));
230 break;
231 default:
232 AssertMsgFailed (("Incorrect SF Dialog type\n"));
233 }
234 QVBoxLayout *mainLayout = new QVBoxLayout (this, 10, 10, "mainLayout");
235
236 /* Setup Input layout */
237 QGridLayout *inputLayout = new QGridLayout (mainLayout, 3, 3, 10, "inputLayout");
238 QLabel *lbPath = new QLabel (tr ("Folder Path"), this);
239 mLePath = new QLineEdit (this);
240 QToolButton *tbPath = new QToolButton (this);
241 QLabel *lbName = new QLabel (tr ("Folder Name"), this);
242 mLeName = new QLineEdit (this);
243 tbPath->setIconSet (VBoxGlobal::iconSet ("select_file_16px.png",
244 "select_file_dis_16px.png"));
245 tbPath->setFocusPolicy (QWidget::TabFocus);
246 connect (mLePath, SIGNAL (textChanged (const QString &)),
247 this, SLOT (validate()));
248 connect (mLeName, SIGNAL (textChanged (const QString &)),
249 this, SLOT (validate()));
250 connect (tbPath, SIGNAL (clicked()), this, SLOT (showFileDialog()));
251 QWhatsThis::add (mLePath, tr ("Displays the path to an existing folder on the host PC."));
252 QWhatsThis::add (mLeName, tr ("Displays the name of the shared folder "
253 "(as it will be seen by the guest OS)."));
254 QWhatsThis::add (tbPath, tr ("Opens the dialog to select a folder."));
255
256 inputLayout->addWidget (lbPath, 0, 0);
257 inputLayout->addWidget (mLePath, 0, 1);
258 inputLayout->addWidget (tbPath, 0, 2);
259 inputLayout->addWidget (lbName, 1, 0);
260 inputLayout->addMultiCellWidget (mLeName, 1, 1, 1, 2);
261
262 mCbReadonly = new QCheckBox (tr ("&Read-only"), this);
263 QWhatsThis::add (mCbReadonly,
264 tr ("When checked, the guest OS will not be able to write to the "
265 "specified shared folder."));
266 mCbReadonly->setChecked (false);
267 inputLayout->addMultiCellWidget (mCbReadonly, 2, 2, 0, 2);
268
269 if (aEnableSelector)
270 {
271 mCbPermanent = new QCheckBox (tr ("&Make Permanent"), this);
272 mCbPermanent->setChecked (true);
273 inputLayout->addMultiCellWidget (mCbPermanent, 3, 3, 0, 2);
274 connect (mCbPermanent, SIGNAL (toggled (bool)),
275 this, SLOT (validate()));
276 }
277
278 /* Setup Button layout */
279 QHBoxLayout *buttonLayout = new QHBoxLayout (mainLayout, 10, "buttonLayout");
280 mBtOk = new QPushButton (tr ("&OK"), this, "btOk");
281 QSpacerItem *spacer = new QSpacerItem (0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
282 QPushButton *btCancel = new QPushButton (tr ("Cancel"), this, "btCancel");
283 connect (mBtOk, SIGNAL (clicked()), this, SLOT (accept()));
284 connect (btCancel, SIGNAL (clicked()), this, SLOT (reject()));
285
286 buttonLayout->addWidget (mBtOk);
287 buttonLayout->addItem (spacer);
288 buttonLayout->addWidget (btCancel);
289
290 /* Validate fields */
291 validate();
292 }
293
294 ~VBoxAddSFDialog() {}
295
296 QString getPath() { return mLePath->text(); }
297 QString getName() { return mLeName->text(); }
298 bool getPermanent()
299 {
300 return mCbPermanent ? mCbPermanent->isChecked() : true;
301 }
302 bool getWritable() { return !mCbReadonly->isChecked(); }
303
304 void setPath (const QString &aPath) { mLePath->setText (aPath); }
305 void setName (const QString &aName) { mLeName->setText (aName); }
306 void setPermanent (bool aPermanent)
307 {
308 if (mCbPermanent)
309 {
310 mCbPermanent->setChecked (aPermanent);
311 mCbPermanent->setEnabled (!aPermanent);
312 }
313 }
314 void setWritable (bool aWritable) { mCbReadonly->setChecked (!aWritable); }
315
316private slots:
317
318 void validate()
319 {
320 VBoxSharedFoldersSettings::SFDialogType dlgType =
321 (VBoxSharedFoldersSettings::SFDialogType)
322 static_cast<VBoxSharedFoldersSettings*> (parent())->dialogType();
323 VBoxSharedFoldersSettings::SFDialogType resultType =
324 mCbPermanent && !mCbPermanent->isChecked() ?
325 VBoxSharedFoldersSettings::ConsoleType :
326 dlgType & VBoxSharedFoldersSettings::MachineType ?
327 VBoxSharedFoldersSettings::MachineType :
328 VBoxSharedFoldersSettings::GlobalType;
329 SFolderName pair = qMakePair (mLeName->text(), resultType);
330
331 mBtOk->setEnabled (!mLePath->text().isEmpty() &&
332 !mLeName->text().stripWhiteSpace().isEmpty() &&
333 (mLeName->text().contains(" ") == 0) &&
334 !mUsedNames.contains (pair));
335 }
336
337 void showFileDialog()
338 {
339 QString folder = vboxGlobal()
340 .getExistingDirectory (QDir::rootDirPath(),
341 this, "addSharedFolderDialog",
342 tr ("Select a folder to share"));
343 if (folder.isNull())
344 return;
345
346 QString folderName = QDir::convertSeparators (folder);
347 QRegExp commonRule ("[\\\\/]([^\\\\^/]+)[\\\\/]?$");
348 QRegExp rootRule ("(([a-zA-Z])[^\\\\^/])?[\\\\/]$");
349 if (commonRule.search (folderName) != -1)
350 {
351 /* processing non-root folder */
352 mLePath->setText (folderName.remove (QRegExp ("[\\\\/]$")));
353 mLeName->setText (commonRule.cap (1));
354 }
355 else if (rootRule.search (folderName) != -1)
356 {
357 /* processing root folder */
358 mLePath->setText (folderName);
359#if defined (Q_OS_WIN) || defined (Q_OS_OS2)
360 mLeName->setText (rootRule.cap (2) + "_DRIVE");
361#elif defined (Q_OS_UNIX)
362 mLeName->setText ("ROOT");
363#endif
364 }
365 else
366 return; /* hm, what type of folder it was? */
367 }
368
369private:
370
371 void showEvent (QShowEvent *aEvent)
372 {
373 setFixedHeight (height());
374 QDialog::showEvent (aEvent);
375 }
376
377 QPushButton *mBtOk;
378 QLineEdit *mLePath;
379 QLineEdit *mLeName;
380 QCheckBox *mCbPermanent;
381 QCheckBox *mCbReadonly;
382 SFoldersNameList mUsedNames;
383};
384
385
386void VBoxSharedFoldersSettings::init()
387{
388 mDialogType = WrongType;
389 listView->setSorting (0);
390 new QIListViewSelectionPreserver (this, listView);
391 listView->setShowToolTips (false);
392 listView->setRootIsDecorated (true);
393 listView->header()->setMovingEnabled (false);
394 tbAdd->setIconSet (VBoxGlobal::iconSet ("add_shared_folder_16px.png",
395 "add_shared_folder_disabled_16px.png"));
396 tbEdit->setIconSet (VBoxGlobal::iconSet ("edit_shared_folder_16px.png",
397 "edit_shared_folder_disabled_16px.png"));
398 tbRemove->setIconSet (VBoxGlobal::iconSet ("revome_shared_folder_16px.png",
399 "revome_shared_folder_disabled_16px.png"));
400 connect (tbAdd, SIGNAL (clicked()), this, SLOT (tbAddPressed()));
401 connect (tbEdit, SIGNAL (clicked()), this, SLOT (tbEditPressed()));
402 connect (tbRemove, SIGNAL (clicked()), this, SLOT (tbRemovePressed()));
403 connect (listView, SIGNAL (currentChanged (QListViewItem *)),
404 this, SLOT (processCurrentChanged (QListViewItem *)));
405
406 /* Make after-paining list update to ensure all columns repainted correctly. */
407 connect (listView->header(), SIGNAL (sizeChange (int, int, int)),
408 this, SLOT (updateList()));
409
410 mIsListViewChanged = false;
411
412 listView->viewport()->installEventFilter (this);
413
414 mTrFull = tr ("Full");
415 mTrReadOnly = tr ("Read-only");
416}
417
418void VBoxSharedFoldersSettings::showEvent (QShowEvent *aEvent)
419{
420 QWidget::showEvent (aEvent);
421
422 /* Adjusting size after all pending show events are processed. */
423 QTimer::singleShot (0, this, SLOT (adjustList()));
424}
425
426
427void VBoxSharedFoldersSettings::updateList()
428{
429 /* Updating list after all pending cell-repaint enevts. */
430 QTimer::singleShot (0, listView, SLOT (updateContents()));
431}
432
433void VBoxSharedFoldersSettings::adjustList()
434{
435 /* Adjust two columns size.
436 * Watching columns 0&2 to feat 1/3 of total width. */
437 int total = listView->viewport()->width();
438
439 listView->adjustColumn (0);
440 int w0 = listView->columnWidth (0) < total / 3 ?
441 listView->columnWidth (0) : total / 3;
442
443 listView->adjustColumn (2);
444 int w2 = listView->columnWidth (2) < total / 3 ?
445 listView->columnWidth (2) : total / 3;
446
447 /* We are adjusting columns 0 and 2 and resizing column 1 to feat
448 * visible listView' width according two adjusted columns. Due to
449 * adjusting column 2 influent column 0 restoring all widths. */
450 listView->setColumnWidth (0, w0);
451 listView->setColumnWidth (1, total - w0 - w2);
452 listView->setColumnWidth (2, w2);
453}
454
455bool VBoxSharedFoldersSettings::eventFilter (QObject *aObject, QEvent *aEvent)
456{
457 /* Process & show auto Tool-Tip for partially hidden listview items. */
458 if (aObject == listView->viewport() && aEvent->type() == QEvent::MouseMove)
459 {
460 QMouseEvent *e = static_cast<QMouseEvent*> (aEvent);
461 QListViewItem *i = listView->itemAt (e->pos());
462 VBoxRichListItem *item = i && i->rtti() == VBoxRichListItem::QIRichListItemId ?
463 static_cast<VBoxRichListItem*> (i) : 0;
464 if (item)
465 {
466 int delta = e->pos().x();
467 int id = 0;
468 for (; id < listView->columns(); ++ id)
469 {
470 if (delta < listView->columnWidth (id))
471 break;
472 delta -= listView->columnWidth (id);
473 }
474
475 QString curText = QToolTip::textFor (listView->viewport());
476 QString newText = item->text (id) != item->getText (id) ?
477 item->getText (id) : QString::null;
478
479 if (newText != curText)
480 {
481 QToolTip::remove (listView->viewport());
482 QToolTip::add (listView->viewport(), newText);
483 }
484 }
485 else
486 QToolTip::remove (listView->viewport());
487 }
488
489 return QWidget::eventFilter (aObject, aEvent);
490}
491
492void VBoxSharedFoldersSettings::setDialogType (int aType)
493{
494 mDialogType = aType;
495}
496
497
498void VBoxSharedFoldersSettings::removeSharedFolder (const QString & aName,
499 const QString & aPath,
500 SFDialogType aType)
501{
502 switch (aType)
503 {
504 case GlobalType:
505 {
506 /* This feature is not implemented yet */
507 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
508 break;
509 }
510 case MachineType:
511 {
512 Assert (!mMachine.isNull());
513 mMachine.RemoveSharedFolder (aName);
514 if (!mMachine.isOk())
515 vboxProblem().cannotRemoveSharedFolder (this, mMachine,
516 aName, aPath);
517 break;
518 }
519 case ConsoleType:
520 {
521 Assert (!mConsole.isNull());
522 mConsole.RemoveSharedFolder (aName);
523 if (!mConsole.isOk())
524 vboxProblem().cannotRemoveSharedFolder (this, mConsole,
525 aName, aPath);
526 break;
527 }
528 default:
529 {
530 AssertMsgFailed (("Incorrect shared folder type\n"));
531 }
532 }
533}
534
535void VBoxSharedFoldersSettings::createSharedFolder (const QString & aName,
536 const QString & aPath,
537 bool aWritable,
538 SFDialogType aType)
539{
540 switch (aType)
541 {
542 case GlobalType:
543 {
544 /* This feature is not implemented yet */
545 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
546 break;
547 }
548 case MachineType:
549 {
550 Assert (!mMachine.isNull());
551 mMachine.CreateSharedFolder (aName, aPath, aWritable);
552 if (!mMachine.isOk())
553 vboxProblem().cannotCreateSharedFolder (this, mMachine,
554 aName, aPath);
555 break;
556 }
557 case ConsoleType:
558 {
559 Assert (!mConsole.isNull());
560 mConsole.CreateSharedFolder (aName, aPath, aWritable);
561 if (!mConsole.isOk())
562 vboxProblem().cannotCreateSharedFolder (this, mConsole,
563 aName, aPath);
564 break;
565 }
566 default:
567 {
568 AssertMsgFailed (("Incorrect shared folder type\n"));
569 }
570 }
571}
572
573
574void VBoxSharedFoldersSettings::getFromGlobal()
575{
576 /* This feature is not implemented yet */
577 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
578
579 /*
580 QString name = tr (" Global Folders");
581 QString key (QString::number (GlobalType));
582 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
583 listView, name, QString::null, QString::null, key);
584 getFrom (vboxGlobal().virtualBox().GetSharedFolders().Enumerate(), root);
585 */
586}
587
588void VBoxSharedFoldersSettings::getFromMachine (const CMachine &aMachine)
589{
590 mMachine = aMachine;
591 QString name = tr (" Machine Folders");
592 QString key (QString::number (MachineType));
593 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
594 listView, name, QString::null, QString::null, key);
595 getFrom (mMachine.GetSharedFolders().Enumerate(), root);
596}
597
598void VBoxSharedFoldersSettings::getFromConsole (const CConsole &aConsole)
599{
600 mConsole = aConsole;
601 QString name = tr (" Transient Folders");
602 QString key (QString::number (ConsoleType));
603 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
604 listView, name, QString::null, QString::null, key);
605 getFrom (mConsole.GetSharedFolders().Enumerate(), root);
606}
607
608void VBoxSharedFoldersSettings::getFrom (const CSharedFolderEnumerator &aEn,
609 QListViewItem *aRoot)
610{
611 aRoot->setSelectable (false);
612 while (aEn.HasMore())
613 {
614 CSharedFolder sf = aEn.GetNext();
615 new VBoxRichListItem (VBoxRichListItem::EllipsisFile, aRoot,
616 sf.GetName(), sf.GetHostPath(),
617 sf.GetWritable() ? mTrFull : mTrReadOnly,
618 "not edited");
619 }
620 listView->setOpen (aRoot, true);
621 listView->setCurrentItem (aRoot->firstChild() ? aRoot->firstChild() : aRoot);
622 processCurrentChanged (aRoot->firstChild() ? aRoot->firstChild() : aRoot);
623}
624
625
626void VBoxSharedFoldersSettings::putBackToGlobal()
627{
628 /* This feature is not implemented yet */
629 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
630
631 /*
632 if (!mIsListViewChanged) return;
633 // This function is only available for GlobalType dialog
634 Assert (mDialogType == GlobalType);
635 // Searching for GlobalType item's root
636 QListViewItem *root = listView->findItem (QString::number (GlobalType), 3);
637 Assert (root);
638 CSharedFolderEnumerator en = vboxGlobal().virtualBox().GetSharedFolders().Enumerate();
639 putBackTo (en, root);
640 */
641}
642
643void VBoxSharedFoldersSettings::putBackToMachine()
644{
645 if (!mIsListViewChanged)
646 return;
647
648 /* This function is only available for MachineType dialog */
649 Assert (mDialogType & MachineType);
650 /* Searching for MachineType item's root */
651 QListViewItem *root = listView->findItem (QString::number (MachineType), 3);
652 Assert (root);
653 CSharedFolderEnumerator en = mMachine.GetSharedFolders().Enumerate();
654 putBackTo (en, root);
655}
656
657void VBoxSharedFoldersSettings::putBackToConsole()
658{
659 if (!mIsListViewChanged)
660 return;
661
662 /* This function is only available for ConsoleType dialog */
663 Assert (mDialogType & ConsoleType);
664 /* Searching for ConsoleType item's root */
665 QListViewItem *root = listView->findItem (QString::number (ConsoleType), 3);
666 Assert (root);
667 CSharedFolderEnumerator en = mConsole.GetSharedFolders().Enumerate();
668 putBackTo (en, root);
669}
670
671void VBoxSharedFoldersSettings::putBackTo (CSharedFolderEnumerator &aEn,
672 QListViewItem *aRoot)
673{
674 Assert (!aRoot->text (3).isNull());
675 SFDialogType type = (SFDialogType)aRoot->text (3).toInt();
676
677 /* deleting all changed folders of the list */
678 while (aEn.HasMore())
679 {
680 CSharedFolder sf = aEn.GetNext();
681
682 /* Search for this root's items */
683 QListViewItem *firstItem = aRoot->firstChild();
684 VBoxRichListItem *item = firstItem &&
685 firstItem->rtti() == VBoxRichListItem::QIRichListItemId ?
686 static_cast<VBoxRichListItem*> (firstItem) : 0;
687 while (item)
688 {
689 if (item->getText (0) == sf.GetName() &&
690 item->getText (1) == sf.GetHostPath() &&
691 item->getText (3) == "not edited")
692 break;
693 item = item->nextSibling();
694 }
695 if (item)
696 continue;
697 removeSharedFolder (sf.GetName(), sf.GetHostPath(), type);
698 }
699
700 /* saving all machine related list view items */
701 QListViewItem *iterator = aRoot->firstChild();
702 while (iterator)
703 {
704 VBoxRichListItem *item = 0;
705 if (iterator->rtti() == VBoxRichListItem::QIRichListItemId)
706 item = static_cast<VBoxRichListItem*> (iterator);
707 if (item && !item->getText (0).isNull() && !item->getText (1).isNull()
708 && item->getText (3) == "edited")
709 createSharedFolder (item->getText (0), item->getText (1),
710 item->getText (2) == mTrFull ? true : false, type);
711 iterator = iterator->nextSibling();
712 }
713}
714
715
716QListViewItem* VBoxSharedFoldersSettings::searchRoot (bool aIsPermanent)
717{
718 if (!aIsPermanent)
719 return listView->findItem (QString::number (ConsoleType), 3);
720 else if (mDialogType & MachineType)
721 return listView->findItem (QString::number (MachineType), 3);
722 else
723 return listView->findItem (QString::number (GlobalType), 3);
724}
725
726void VBoxSharedFoldersSettings::tbAddPressed()
727{
728 /* Make the used names list: */
729 SFoldersNameList usedList;
730 QListViewItemIterator it (listView);
731 while (*it)
732 {
733 if ((*it)->parent() && (*it)->rtti() == VBoxRichListItem::QIRichListItemId)
734 {
735 VBoxRichListItem *item = static_cast<VBoxRichListItem*> (*it);
736 SFDialogType type = (SFDialogType)item->parent()->text (3).toInt();
737 usedList << qMakePair (item->getText (0), type);
738 }
739 ++ it;
740 }
741
742 /* Invoke Add-Box Dialog */
743 VBoxAddSFDialog dlg (this, VBoxAddSFDialog::AddDialogType,
744 mDialogType & ConsoleType, usedList);
745 if (dlg.exec() != QDialog::Accepted)
746 return;
747 QString name = dlg.getName();
748 QString path = dlg.getPath();
749 bool isPermanent = dlg.getPermanent();
750 /* Shared folder's name & path could not be empty */
751 Assert (!name.isEmpty() && !path.isEmpty());
752 /* Searching root for the new listview item */
753 QListViewItem *root = searchRoot (isPermanent);
754 Assert (root);
755 /* Appending a new listview item to the root */
756 VBoxRichListItem *item = new VBoxRichListItem (
757 VBoxRichListItem::EllipsisFile, root, name, path,
758 dlg.getWritable() ? mTrFull : mTrReadOnly, "edited");
759 /* Make the created item selected */
760 listView->ensureItemVisible (item);
761 listView->setCurrentItem (item);
762 processCurrentChanged (item);
763 listView->setFocus();
764 adjustList();
765
766 mIsListViewChanged = true;
767}
768
769void VBoxSharedFoldersSettings::tbEditPressed()
770{
771 /* Make the used names list: */
772 SFoldersNameList usedList;
773 QListViewItemIterator it (listView);
774 while (*it)
775 {
776 if ((*it)->parent() && !(*it)->isSelected() &&
777 (*it)->rtti() == VBoxRichListItem::QIRichListItemId)
778 {
779 VBoxRichListItem *item = static_cast<VBoxRichListItem*> (*it);
780 SFDialogType type = (SFDialogType)item->parent()->text (3).toInt();
781 usedList << qMakePair (item->getText (0), type);
782 }
783 ++ it;
784 }
785
786 /* Check selected item */
787 QListViewItem *selectedItem = listView->selectedItem();
788 VBoxRichListItem *item =
789 selectedItem->rtti() == VBoxRichListItem::QIRichListItemId ?
790 static_cast<VBoxRichListItem*> (selectedItem) : 0;
791 Assert (item);
792 Assert (item->parent());
793 /* Invoke Edit-Box Dialog */
794 VBoxAddSFDialog dlg (this, VBoxAddSFDialog::EditDialogType,
795 mDialogType & ConsoleType, usedList);
796 dlg.setPath (item->getText (1));
797 dlg.setName (item->getText (0));
798 dlg.setPermanent ((SFDialogType)item->parent()->text (3).toInt()
799 != ConsoleType);
800 dlg.setWritable (item->getText (2) == mTrFull);
801 if (dlg.exec() != QDialog::Accepted)
802 return;
803 QString name = dlg.getName();
804 QString path = dlg.getPath();
805 bool isPermanent = dlg.getPermanent();
806 /* Shared folder's name & path could not be empty */
807 Assert (!name.isEmpty() && !path.isEmpty());
808 /* Searching new root for the selected listview item */
809 QListViewItem *root = searchRoot (isPermanent);
810 Assert (root);
811 /* Updating an edited listview item */
812 item->updateText (3, "edited");
813 item->updateText (2, dlg.getWritable() ? mTrFull : mTrReadOnly);
814 item->updateText (1, path);
815 item->updateText (0, name);
816 if (item->parent() != root)
817 {
818 /* Move the selected item into new location */
819 item->parent()->takeItem (item);
820 root->insertItem (item);
821
822 /* Make the created item selected */
823 listView->ensureItemVisible (item);
824 listView->setCurrentItem (item);
825 processCurrentChanged (item);
826 listView->setFocus();
827 }
828 item->repaint();
829 adjustList();
830
831 mIsListViewChanged = true;
832}
833
834void VBoxSharedFoldersSettings::tbRemovePressed()
835{
836 Assert (listView->selectedItem());
837 delete listView->selectedItem();
838 adjustList();
839 mIsListViewChanged = true;
840}
841
842
843void VBoxSharedFoldersSettings::processCurrentChanged (QListViewItem *aItem)
844{
845 if (aItem && aItem->isSelectable() && listView->selectedItem() != aItem)
846 listView->setSelected (aItem, true);
847 bool addEnabled = aItem &&
848 (isEditable (aItem->text (3)) ||
849 (aItem->parent() && isEditable (aItem->parent()->text (3))));
850 bool removeEnabled = aItem && aItem->parent() &&
851 isEditable (aItem->parent()->text (3));
852 tbAdd->setEnabled (addEnabled);
853 tbEdit->setEnabled (removeEnabled);
854 tbRemove->setEnabled (removeEnabled);
855}
856
857void VBoxSharedFoldersSettings::processDoubleClick (QListViewItem *aItem)
858{
859 bool editEnabled = aItem && aItem->parent() &&
860 isEditable (aItem->parent()->text (3));
861 if (editEnabled)
862 tbEditPressed();
863}
864
865bool VBoxSharedFoldersSettings::isEditable (const QString &aKey)
866{
867 /* mDialogType should be setup already */
868 Assert (mDialogType);
869
870 SFDialogType type = (SFDialogType)aKey.toInt();
871 if (!type) return false;
872 return mDialogType & type;
873}
874
875
876#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