1 /***************************************************************************
2     Private helper class(es) for the profile manager.
3                              -------------------
4     begin                : Mi Aug 12 2014
5     copyright            : (C) 2014-2019 by Alexander Reinholdt
6     email                : alexander.reinholdt@kdemail.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful, but   *
16  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
18  *   General Public License for more details.                              *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
23  *   MA 02110-1335, USA                                                    *
24  ***************************************************************************/
25 
26 // application specific includes
27 #include "smb4kprofilemanager_p.h"
28 #include "smb4ksettings.h"
29 
30 // Qt includes
31 #include <QVBoxLayout>
32 #include <QHBoxLayout>
33 #include <QGridLayout>
34 #include <QLabel>
35 #include <QDialogButtonBox>
36 #include <QPixmap>
37 #include <QWindow>
38 
39 // KDE includes
40 #define TRANSLATION_DOMAIN "smb4k-core"
41 #include <KI18n/KLocalizedString>
42 #include <KIconThemes/KIconLoader>
43 #include <KConfigGui/KWindowConfig>
44 
45 
Smb4KProfileMigrationDialog(const QStringList & from,const QStringList & to,QWidget * parent)46 Smb4KProfileMigrationDialog::Smb4KProfileMigrationDialog(const QStringList& from, const QStringList& to, QWidget* parent)
47 : QDialog(parent), m_from_list(from), m_to_list(to)
48 {
49   //
50   // Set the window title
51   //
52   setWindowTitle(i18n("Profile Migration Assistant"));
53 
54   //
55   // Setup the view
56   //
57   setupView();
58 
59   //
60   // Set the dialog size
61   //
62   create();
63 
64   KConfigGroup group(Smb4KSettings::self()->config(), "ProfileMigrationDialog");
65   QSize dialogSize;
66 
67   if (group.exists())
68   {
69     KWindowConfig::restoreWindowSize(windowHandle(), group);
70     dialogSize = windowHandle()->size();
71   }
72   else
73   {
74     dialogSize = sizeHint();
75   }
76 
77   resize(dialogSize); // workaround for QTBUG-40584
78 }
79 
80 
~Smb4KProfileMigrationDialog()81 Smb4KProfileMigrationDialog::~Smb4KProfileMigrationDialog()
82 {
83 }
84 
85 
setupView()86 void Smb4KProfileMigrationDialog::setupView()
87 {
88   //
89   // The layout
90   //
91   QVBoxLayout *layout = new QVBoxLayout(this);
92 
93   //
94   // The description
95   //
96   QWidget *description = new QWidget(this);
97   QHBoxLayout *descriptionLayout = new QHBoxLayout(description);
98   descriptionLayout->setContentsMargins(0, 0, 0, 0);
99 
100   QLabel *pixmap = new QLabel(description);
101   QPixmap pix = KDE::icon("format-list-unordered").pixmap(KIconLoader::SizeHuge);
102   pixmap->setPixmap(pix);
103   pixmap->setAlignment(Qt::AlignBottom);
104 
105   QLabel *label = new QLabel(i18n("Migrate all relevant settings of one profile to another."));
106   label->setWordWrap(true);
107   label->setAlignment(Qt::AlignBottom);
108 
109   descriptionLayout->addWidget(pixmap, 0);
110   descriptionLayout->addWidget(label, Qt::AlignBottom);
111 
112   //
113   // The input widgets
114   //
115   QWidget *editors = new QWidget(this);
116   QGridLayout *editorsLayout = new QGridLayout(editors);
117   editorsLayout->setSpacing(5);
118   editorsLayout->setContentsMargins(0, 0, 0, 0);
119   editorsLayout->setColumnStretch(0, 0);
120   editorsLayout->setColumnStretch(1, 1);
121 
122   QLabel *from = new QLabel(i18n("Old Profile:"), editors);
123   editorsLayout->addWidget(from, 0, 0);
124 
125   m_from_box = new KComboBox(editors);
126 
127   if (m_from_list.size() == 1 && m_from_list.first().isEmpty())
128   {
129     m_from_box->addItem(i18n("<Default Profile>"));
130   }
131   else
132   {
133     if (m_to_list.size() == 1 && m_to_list.first().isEmpty())
134     {
135       m_from_box->addItem(i18n("<All Profiles>"));
136     }
137     else
138     {
139       m_from_box->addItems(m_from_list);
140     }
141   }
142 
143   editorsLayout->addWidget(m_from_box, 0, 1);
144 
145   QLabel *to = new QLabel(i18n("New Profile:"), editors);
146   editorsLayout->addWidget(to, 1, 0);
147 
148   m_to_box = new KComboBox(editors);
149 
150   if (m_to_list.size() == 1 && m_to_list.first().isEmpty())
151   {
152     m_to_box->addItem(i18n("<Default Profile>"));
153   }
154   else
155   {
156     m_to_box->addItems(m_to_list);
157     m_to_box->setCurrentText(Smb4KProfileManager::self()->activeProfile());
158   }
159 
160   editorsLayout->addWidget(m_to_box, 1, 1);
161 
162   QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
163   m_ok_button = buttonBox->addButton(QDialogButtonBox::Ok);
164   m_cancel_button = buttonBox->addButton(QDialogButtonBox::Cancel);
165 
166   m_ok_button->setShortcut(Qt::CTRL|Qt::Key_Return);
167   m_cancel_button->setShortcut(Qt::Key_Escape);
168 
169   m_ok_button->setDefault(true);
170   m_ok_button->setEnabled(!m_to_box->currentText().isEmpty());
171 
172   layout->addWidget(description, 0);
173   layout->addWidget(editors, 0);
174   layout->addWidget(buttonBox, 0);
175 
176   connect(m_ok_button, SIGNAL(clicked()), this, SLOT(slotOkClicked()));
177   connect(m_cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
178 }
179 
180 
from() const181 QString Smb4KProfileMigrationDialog::from() const
182 {
183   if (m_from_box->currentText() == i18n("<Default Profile>"))
184   {
185     return QString();
186   }
187 
188   return m_from_box->currentText();
189 }
190 
191 
to() const192 QString Smb4KProfileMigrationDialog::to() const
193 {
194   if (m_to_box->currentText() == i18n("<Default Profile>"))
195   {
196     return QString();
197   }
198 
199   return m_to_box->currentText();
200 }
201 
202 
slotOkClicked()203 void Smb4KProfileMigrationDialog::slotOkClicked()
204 {
205   KConfigGroup group(Smb4KSettings::self()->config(), "ProfileMigrationDialog");
206   KWindowConfig::saveWindowSize(windowHandle(), group);
207   accept();
208 }
209 
210