VirtualBox

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

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

Small fixes for Shared Folders list:
ignoring width of sub-headers (for column adjusting).

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