VirtualBox

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

Last change on this file since 2175 was 2139, checked in by vboxsync, 18 years ago

FE/Qt: Added new translation files, corrected some spelling in sources, translations synchronized with sources.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.2 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 (tr ("Folder Path"), this);
178 mLePath = new QLineEdit (this);
179 QToolButton *tbPath = new QToolButton (this);
180 QLabel *lbName = new QLabel (tr ("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 ("Displays the path to an existing folder on the host PC."));
191 QWhatsThis::add (mLeName, tr ("Displays the name of the shared folder "
192 "(as it will be seen by the guest OS)."));
193 QWhatsThis::add (tbPath, tr ("Opens the dialog to select a folder."));
194
195 inputLayout->addWidget (lbPath, 0, 0);
196 inputLayout->addWidget (mLePath, 0, 1);
197 inputLayout->addWidget (tbPath, 0, 2);
198 inputLayout->addWidget (lbName, 1, 0);
199 inputLayout->addMultiCellWidget (mLeName, 1, 1, 1, 2);
200
201 /* Setup Button layout */
202 QHBoxLayout *buttonLayout = new QHBoxLayout (mainLayout, 10, "buttonLayout");
203 mBtOk = new QPushButton (tr ("OK"), this, "btOk");
204 QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
205 QPushButton *btCancel = new QPushButton (tr ("Cancel"), this, "btCancel");
206 connect (mBtOk, SIGNAL (clicked()), this, SLOT (accept()));
207 connect (btCancel, SIGNAL (clicked()), this, SLOT (reject()));
208
209 buttonLayout->addWidget (mBtOk);
210 buttonLayout->addItem (spacer);
211 buttonLayout->addWidget (btCancel);
212
213 /* Validate fields */
214 validate();
215 }
216
217 ~VBoxAddSFDialog() {}
218
219 QString getPath() { return mLePath->text(); }
220 QString getName() { return mLeName->text(); }
221
222 void setPath (const QString &aPath) { mLePath->setText (aPath); }
223 void setName (const QString &aName) { mLeName->setText (aName); }
224
225private slots:
226
227 void validate()
228 {
229 mBtOk->setEnabled (!mLePath->text().isEmpty() && !mLeName->text().isEmpty());
230 }
231
232 void showFileDialog()
233 {
234 QString folder = vboxGlobal().getExistingDirectory (QDir::rootDirPath(),
235 this, "addSharedFolderDialog",
236 tr ("Select a folder to share"));
237 if (folder.isNull())
238 return;
239
240 QString folderName = QDir::convertSeparators (folder);
241 QRegExp commonRule ("[\\\\/]([^\\\\^/]+)[\\\\/]?$");
242 QRegExp rootRule ("(([a-zA-Z])[^\\\\^/])?[\\\\/]$");
243 if (commonRule.search (folderName) != -1)
244 {
245 /* processing non-root folder */
246 mLePath->setText (folderName.remove (QRegExp ("[\\\\/]$")));
247 mLeName->setText (commonRule.cap (1));
248 }
249 else if (rootRule.search (folderName) != -1)
250 {
251 /* processing root folder */
252 mLePath->setText (folderName);
253#if defined(Q_WS_WIN32)
254 mLeName->setText (rootRule.cap (2) + "_DRIVE");
255#elif defined(Q_WS_X11)
256 mLeName->setText ("ROOT");
257#endif
258 }
259 else
260 return; /* hm, what type of folder it was? */
261 }
262
263private:
264
265 void showEvent (QShowEvent *aEvent)
266 {
267 setFixedHeight (height());
268 QDialog::showEvent (aEvent);
269 }
270
271 QPushButton *mBtOk;
272 QLineEdit *mLePath;
273 QLineEdit *mLeName;
274};
275
276
277void VBoxSharedFoldersSettings::init()
278{
279 mDialogType = WrongType;
280 new QIListViewSelectionPreserver (this, listView);
281 listView->setShowToolTips (false);
282 listView->setRootIsDecorated (true);
283 tbAdd->setIconSet (VBoxGlobal::iconSet ("add_shared_folder_16px.png",
284 "add_shared_folder_disabled_16px.png"));
285 tbEdit->setIconSet (VBoxGlobal::iconSet ("edit_shared_folder_16px.png",
286 "edit_shared_folder_disabled_16px.png"));
287 tbRemove->setIconSet (VBoxGlobal::iconSet ("revome_shared_folder_16px.png",
288 "revome_shared_folder_disabled_16px.png"));
289 QToolTip::add (tbAdd, tr ("Add a new shared folder"));
290 QToolTip::add (tbEdit, tr ("Edit the selected shared folder"));
291 QToolTip::add (tbRemove, tr ("Remove the selected shared folder"));
292 connect (tbAdd, SIGNAL (clicked()), this, SLOT (tbAddPressed()));
293 connect (tbEdit, SIGNAL (clicked()), this, SLOT (tbEditPressed()));
294 connect (tbRemove, SIGNAL (clicked()), this, SLOT (tbRemovePressed()));
295 connect (listView, SIGNAL (currentChanged (QListViewItem *)),
296 this, SLOT (processCurrentChanged (QListViewItem *)));
297 connect (listView, SIGNAL (onItem (QListViewItem *)),
298 this, SLOT (processOnItem (QListViewItem *)));
299
300 mIsListViewChanged = false;
301}
302
303void VBoxSharedFoldersSettings::setDialogType (
304 VBoxSharedFoldersSettings::SFDialogType aType)
305{
306 mDialogType = aType;
307}
308
309
310void VBoxSharedFoldersSettings::removeSharedFolder (const QString & aName,
311 const QString & aPath,
312 SFDialogType aType)
313{
314 switch (aType)
315 {
316 case GlobalType:
317 {
318 /* This feature is not implemented yet */
319 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
320 break;
321 }
322 case MachineType:
323 {
324 Assert (!mMachine.isNull());
325 mMachine.RemoveSharedFolder (aName);
326 if (!mMachine.isOk())
327 vboxProblem().cannotRemoveSharedFolder (this, mMachine,
328 aName, aPath);
329 break;
330 }
331 case ConsoleType:
332 {
333 Assert (!mConsole.isNull());
334 mConsole.RemoveSharedFolder (aName);
335 if (!mConsole.isOk())
336 vboxProblem().cannotRemoveSharedFolder (this, mConsole,
337 aName, aPath);
338 break;
339 }
340 default:
341 {
342 AssertMsgFailed (("Incorrect shared folder type\n"));
343 }
344 }
345}
346
347void VBoxSharedFoldersSettings::createSharedFolder (const QString & aName,
348 const QString & aPath,
349 SFDialogType aType)
350{
351 switch (aType)
352 {
353 case GlobalType:
354 {
355 /* This feature is not implemented yet */
356 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
357 break;
358 }
359 case MachineType:
360 {
361 Assert (!mMachine.isNull());
362 mMachine.CreateSharedFolder (aName, aPath);
363 if (!mMachine.isOk())
364 vboxProblem().cannotCreateSharedFolder (this, mMachine,
365 aName, aPath);
366 break;
367 }
368 case ConsoleType:
369 {
370 Assert (!mConsole.isNull());
371 mConsole.CreateSharedFolder (aName, aPath);
372 if (!mConsole.isOk())
373 vboxProblem().cannotCreateSharedFolder (this, mConsole,
374 aName, aPath);
375 break;
376 }
377 default:
378 {
379 AssertMsgFailed (("Incorrect shared folder type\n"));
380 }
381 }
382}
383
384
385void VBoxSharedFoldersSettings::getFromGlobal()
386{
387 /* This feature is not implemented yet */
388 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
389
390 /*
391 QString name = tr (" Global Folders");
392 QString key (QString::number (GlobalType));
393 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
394 listView, name, QString::null, key);
395 getFrom (vboxGlobal().virtualBox().GetSharedFolders().Enumerate(), root);
396 */
397}
398
399void VBoxSharedFoldersSettings::getFromMachine (const CMachine &aMachine)
400{
401 mMachine = aMachine;
402 QString name = tr (" Machine Folders");
403 QString key (QString::number (MachineType));
404 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
405 listView, name, QString::null, key);
406 getFrom (mMachine.GetSharedFolders().Enumerate(), root);
407}
408
409void VBoxSharedFoldersSettings::getFromConsole (const CConsole &aConsole)
410{
411 mConsole = aConsole;
412 QString name = tr (" Transient Folders");
413 QString key (QString::number (ConsoleType));
414 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
415 listView, name, QString::null, key);
416 getFrom (mConsole.GetSharedFolders().Enumerate(), root);
417}
418
419void VBoxSharedFoldersSettings::getFrom (const CSharedFolderEnumerator &aEn,
420 QListViewItem *aRoot)
421{
422 aRoot->setSelectable (false);
423 while (aEn.HasMore())
424 {
425 CSharedFolder sf = aEn.GetNext();
426 new VBoxRichListItem (VBoxRichListItem::EllipsisFile, aRoot,
427 sf.GetName(), sf.GetHostPath());
428 }
429 listView->setOpen (aRoot, true);
430 listView->setCurrentItem (aRoot);
431 processCurrentChanged (aRoot);
432}
433
434
435void VBoxSharedFoldersSettings::putBackToGlobal()
436{
437 /* This feature is not implemented yet */
438 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
439
440 /*
441 if (!mIsListViewChanged) return;
442 // This function is only available for GlobalType dialog
443 Assert (mDialogType == GlobalType);
444 // Searching for GlobalType item's root
445 QListViewItem *root = listView->findItem (QString::number (GlobalType), 2);
446 Assert (root);
447 CSharedFolderEnumerator en = vboxGlobal().virtualBox().GetSharedFolders().Enumerate();
448 putBackTo (en, root);
449 */
450}
451
452void VBoxSharedFoldersSettings::putBackToMachine()
453{
454 if (!mIsListViewChanged)
455 return;
456
457 /* This function is only available for MachineType dialog */
458 Assert (mDialogType == MachineType);
459 /* Searching for MachineType item's root */
460 QListViewItem *root = listView->findItem (QString::number (MachineType), 2);
461 Assert (root);
462 CSharedFolderEnumerator en = mMachine.GetSharedFolders().Enumerate();
463 putBackTo (en, root);
464}
465
466void VBoxSharedFoldersSettings::putBackToConsole()
467{
468 if (!mIsListViewChanged)
469 return;
470
471 /* This function is only available for ConsoleType dialog */
472 Assert (mDialogType == ConsoleType);
473 /* Searching for ConsoleType item's root */
474 QListViewItem *root = listView->findItem (QString::number (ConsoleType), 2);
475 Assert (root);
476 CSharedFolderEnumerator en = mConsole.GetSharedFolders().Enumerate();
477 putBackTo (en, root);
478}
479
480void VBoxSharedFoldersSettings::putBackTo (CSharedFolderEnumerator &aEn,
481 QListViewItem *aRoot)
482{
483 Assert (!aRoot->text (2).isNull());
484 SFDialogType type = (SFDialogType)aRoot->text (2).toInt();
485
486 /* deleting all existing folders if the list */
487 while (aEn.HasMore())
488 {
489 CSharedFolder sf = aEn.GetNext();
490 removeSharedFolder (sf.GetName(), sf.GetHostPath(), type);
491 }
492
493 /* saving all machine related list view items */
494 QListViewItem *iterator = aRoot->firstChild();
495 while (iterator)
496 {
497 VBoxRichListItem *item = 0;
498 if (iterator->rtti() == VBoxRichListItem::QIRichListItemId)
499 item = static_cast<VBoxRichListItem*> (iterator);
500 if (item && !item->getText (0).isNull() && !item->getText (1).isNull())
501 createSharedFolder (item->getText (0), item->getText (1), type);
502 else
503 AssertMsgFailed (("Incorrect listview item type\n"));
504 iterator = iterator->nextSibling();
505 }
506}
507
508
509void VBoxSharedFoldersSettings::tbAddPressed()
510{
511 /* Invoke Add-Box Dialog */
512 VBoxAddSFDialog dlg (this, VBoxAddSFDialog::AddDType);
513 if (dlg.exec() != QDialog::Accepted)
514 return;
515 QString name = dlg.getName();
516 QString path = dlg.getPath();
517 /* Shared folder's name & path could not be empty */
518 Assert (!name.isEmpty() && !path.isEmpty());
519 /* Searching root for the new listview item */
520 QListViewItem *root = listView->findItem (QString::number (mDialogType), 2);
521 Assert (root);
522 /* Appending a new listview item to the root */
523 VBoxRichListItem *item = new VBoxRichListItem (VBoxRichListItem::EllipsisFile,
524 root, name, path);
525 listView->ensureItemVisible (item);
526 listView->setCurrentItem (item);
527 processCurrentChanged (item);
528 listView->setFocus();
529 mIsListViewChanged = true;
530}
531
532void VBoxSharedFoldersSettings::tbEditPressed()
533{
534 /* Check selected item */
535 QListViewItem *selectedItem = listView->selectedItem();
536 VBoxRichListItem *item = 0;
537 if (selectedItem->rtti() == VBoxRichListItem::QIRichListItemId)
538 item = static_cast<VBoxRichListItem*> (selectedItem);
539 Assert (item);
540 /* Invoke Add-Box Dialog */
541 VBoxAddSFDialog dlg (this, VBoxAddSFDialog::EditDType);
542 dlg.setPath (item->getText (1));
543 dlg.setName (item->getText (0));
544 if (dlg.exec() != QDialog::Accepted)
545 return;
546 QString name = dlg.getName();
547 QString path = dlg.getPath();
548 /* Shared folder's name & path could not be empty */
549 Assert (!name.isEmpty() && !path.isEmpty());
550 /* Updating an edited listview item */
551 item->updateText (1, path);
552 item->updateText (0, name);
553 mIsListViewChanged = true;
554}
555
556void VBoxSharedFoldersSettings::tbRemovePressed()
557{
558 Assert (listView->selectedItem());
559 delete listView->selectedItem();
560 mIsListViewChanged = true;
561}
562
563
564void VBoxSharedFoldersSettings::processOnItem (QListViewItem *aItem)
565{
566 VBoxRichListItem *item = 0;
567 if (aItem->rtti() == VBoxRichListItem::QIRichListItemId)
568 item = static_cast<VBoxRichListItem*> (aItem);
569 Assert (item);
570 QString tip = tr ("<nobr>Name:&nbsp;&nbsp;%1</nobr><br>"
571 "<nobr>Path:&nbsp;&nbsp;%2</nobr>")
572 .arg (item->getText (0)).arg (item->getText (1));
573 if (!item->getText (0).isNull() && !item->getText (1).isNull())
574 QToolTip::add (listView->viewport(), listView->itemRect (aItem), tip);
575 else
576 QToolTip::remove (listView->viewport());
577}
578
579void VBoxSharedFoldersSettings::processCurrentChanged (QListViewItem *aItem)
580{
581 if (aItem && aItem->isSelectable() && listView->selectedItem() != aItem)
582 listView->setSelected (aItem, true);
583 bool addEnabled = aItem &&
584 (isEditable (aItem->text (2)) ||
585 aItem->parent() && isEditable (aItem->parent()->text (2)));
586 bool removeEnabled = aItem && aItem->parent() &&
587 isEditable (aItem->parent()->text (2));
588 tbAdd->setEnabled (addEnabled);
589 tbEdit->setEnabled (removeEnabled);
590 tbRemove->setEnabled (removeEnabled);
591}
592
593bool VBoxSharedFoldersSettings::isEditable (const QString &aKey)
594{
595 /* mDialogType should be sutup already */
596 Assert (mDialogType);
597 /* simple item has no key information */
598 if (aKey.isEmpty())
599 return false;
600 SFDialogType type = (SFDialogType)aKey.toInt();
601 if (!type)
602 AssertMsgFailed (("Incorrect listview item key value\n"));
603 return type == mDialogType;
604}
605
606
607#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