Use configured resolution for login/outgame/ingame
[ryzomcore.git] / ryzom / tools / client / ryzom_installer / src / profilesdialog.cpp
blobecb0797b34e74e911b2ea5485af877834df6f648
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2019 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdpch.h"
18 #include "profilesdialog.h"
19 #include "profilesmodel.h"
20 #include "serversmodel.h"
21 #include "operationdialog.h"
22 #include "utils.h"
24 #ifdef DEBUG_NEW
25 #define new DEBUG_NEW
26 #endif
28 CProfilesDialog::CProfilesDialog(QWidget *parent):QDialog(parent), m_currentProfileIndex(-1)
30 setupUi(this);
32 connect(addButton, SIGNAL(clicked()), SLOT(onAddProfile()));
33 connect(deleteButton, SIGNAL(clicked()), SLOT(onDeleteProfile()));
34 connect(profilesListView, SIGNAL(clicked(QModelIndex)), SLOT(onProfileClicked(QModelIndex)));
35 connect(executableDefaultButton, SIGNAL(clicked()), SLOT(onExecutableDefaultClicked()));
36 connect(executableBrowseButton, SIGNAL(clicked()), SLOT(onExecutableBrowseClicked()));
37 connect(directoryButton, SIGNAL(clicked()), SLOT(onProfileDirectoryClicked()));
39 m_model = new CProfilesModel(this);
40 m_serversModel = new CServersModel(this);
42 profilesListView->setModel(m_model);
43 serverComboBox->setModel(m_serversModel);
45 int index = CConfigFile::getInstance()->getDefaultProfileIndex();
47 profilesListView->setCurrentIndex(m_model->index(index, 0));
48 displayProfile(index);
51 CProfilesDialog::~CProfilesDialog()
55 void CProfilesDialog::accept()
57 saveProfile(m_currentProfileIndex);
59 const CProfiles &profiles = m_model->getProfiles();
61 // check if profiles are valid
62 foreach(const CProfile &profile, profiles)
64 QString error;
66 if (!profile.isValid(error))
68 // display an error message
69 QMessageBox::critical(this, tr("Error"), error);
70 return;
74 m_model->save();
76 QDialog::accept();
79 void CProfilesDialog::onAddProfile()
81 addProfile();
84 void CProfilesDialog::onDeleteProfile()
86 QMessageBox::StandardButton res = QMessageBox::question(this, tr("Confirmation"), tr("You're going to delete a profile, all files that belong to it (configuration, saves, logs, screenshots, etc...) will be deleted.\nAre you sure to delete this profile?"));
88 if (res != QMessageBox::Yes) return;
90 QModelIndex index = profilesListView->currentIndex();
92 deleteProfile(index.row());
95 void CProfilesDialog::onProfileClicked(const QModelIndex &index)
97 nlwarning("Clicked on profile %d", index.row());
99 displayProfile(index.row());
102 void CProfilesDialog::displayProfile(int index)
104 bool enabled = index > -1;
106 profileIdLabel->setEnabled(enabled);
107 nameEdit->setEnabled(enabled);
108 serverComboBox->setEnabled(enabled);
109 argumentsEdit->setEnabled(enabled);
110 commentsEdit->setEnabled(enabled);
111 langComboBox->setEnabled(enabled);
113 if (index < 0) return;
115 saveProfile(m_currentProfileIndex);
117 const CProfile &profile = m_model->getProfiles()[index];
119 QString executable = profile.executable;
121 if (executable.isEmpty())
123 executable = profile.getClientFullPath();
126 QString profileDirectory = profile.getDirectory();
128 // update all widgets with content of profile
129 profileIdLabel->setText(profile.id);
130 nameEdit->setText(profile.name);
131 serverComboBox->setCurrentIndex(m_serversModel->getIndexFromServerID(profile.server));
132 executablePathLabel->setText(QFileInfo(executable).fileName());
133 argumentsEdit->setText(profile.arguments);
134 commentsEdit->setPlainText(profile.comments);
135 directoryPathLabel->setText(profileDirectory);
136 desktopShortcutCheckBox->setChecked(profile.desktopShortcut);
137 menuShortcutCheckBox->setChecked(profile.menuShortcut);
138 langComboBox->setCurrentIndex(getIndexFromProfileLanguage(profile.language));
140 // disable click on button if directory doesn't exist
141 directoryButton->setEnabled(QFile::exists(profileDirectory));
143 updateExecutableVersion(index);
145 m_currentProfileIndex = index;
148 void CProfilesDialog::saveProfile(int index)
150 if (index < 0 || index >= m_model->rowCount()) return;
152 CProfile &profile = m_model->getProfiles()[index];
154 profile.name = nameEdit->text();
155 profile.server = m_serversModel->getServerIDFromIndex(serverComboBox->currentIndex());
156 profile.arguments = argumentsEdit->text();
157 profile.comments = commentsEdit->toPlainText();
158 profile.desktopShortcut = desktopShortcutCheckBox->isChecked();
159 profile.menuShortcut = menuShortcutCheckBox->isChecked();
160 profile.language = getProfileLanguageFromIndex(langComboBox->currentIndex());
163 void CProfilesDialog::deleteProfile(int index)
165 if (index < 0) return;
167 m_model->removeRow(index);
169 // decrement profile index
170 --index;
172 // select row and update content
173 profilesListView->setCurrentIndex(m_model->index(index, 0));
174 displayProfile(index);
176 // delete files for delete profile
177 COperationDialog dialog(this);
178 dialog.setOperation(OperationUpdateProfiles);
181 void CProfilesDialog::addProfile()
183 int index = m_model->rowCount();
185 // append the new profile
186 m_model->insertRow(index);
188 CConfigFile *config = CConfigFile::getInstance();
190 CProfile &profile = m_model->getProfiles()[index];
191 const CServer &server = config->getServer(config->getDefaultServerIndex());
193 int nextId = 0;
195 // search an ID that doesn't correspond to an existing profile directory
196 while (QFile::exists(config->getProfileDirectory() + "/" + QString::number(nextId))) ++nextId;
198 // increment this ID until not used in profiles
199 while(nextId < 100)
201 bool found = false;
203 // search if this ID is already used in existing profiles
204 foreach(const CProfile &p, m_model->getProfiles())
206 if (p.id == QString::number(nextId))
208 found = true;
209 break;
213 if (!found) break;
215 // increment ID
216 ++nextId;
219 // set default parameters
220 profile.id = QString::number(nextId);
221 profile.server = server.id;
222 profile.language = config->getLanguage(); // locale
224 profilesListView->setCurrentIndex(m_model->index(index, 0));
225 displayProfile(index);
227 // TODO: copy files to new server if files don't exist
230 void CProfilesDialog::updateExecutableVersion(int index)
232 if (index < 0) return;
234 const CProfile &profile = m_model->getProfiles()[index];
235 const CServer &server = CConfigFile::getInstance()->getServer(profile.server);
237 QString executable = profile.executable;
239 // file empty, use default one
240 if (executable.isEmpty())
242 executable = server.getClientFullPath();
245 // file doesn't exist
246 if (executable.isEmpty() || !QFile::exists(executable)) return;
248 // convert output to string
249 QString versionString = getVersionFromExecutable(executable, server.getDirectory());
251 if (!versionString.isEmpty())
253 executablePathLabel->setText(QString("%1 (%2)").arg(QFileInfo(executable).fileName()).arg(versionString));
257 void CProfilesDialog::onExecutableDefaultClicked()
259 if (m_currentProfileIndex < 0) return;
261 CProfile &profile = m_model->getProfiles()[m_currentProfileIndex];
263 profile.executable.clear();
265 updateExecutableVersion(m_currentProfileIndex);
268 void CProfilesDialog::onExecutableBrowseClicked()
270 if (m_currentProfileIndex < 0) return;
272 CProfile &profile = m_model->getProfiles()[m_currentProfileIndex];
274 QString executable = profile.executable;
275 QString defaultExecutable = CConfigFile::getInstance()->getServer(profile.server).getClientFullPath();
277 if (executable.isEmpty()) executable = defaultExecutable;
279 QString filter;
281 #ifdef Q_OS_WIN32
282 filter = tr("Executables (*.exe)");
283 #else
284 filter = tr("Executables (*)");
285 #endif
287 QFileDialog open;
288 open.setFilter(QDir::Executable | QDir::NoDotAndDotDot | QDir::Files);
290 executable = open.getOpenFileName(this, tr("Please choose Ryzom client executable to launch"), executable, filter);
292 if (executable.isEmpty()) return;
294 // don't need to save the new executable if the same as default one
295 if (executable == defaultExecutable)
297 profile.executable.clear();
299 else
301 profile.executable = executable;
304 executablePathLabel->setText(QFileInfo(executable).fileName());
306 updateExecutableVersion(m_currentProfileIndex);
309 void CProfilesDialog::onProfileDirectoryClicked()
311 if (m_currentProfileIndex < 0) return;
313 const CProfile &profile = m_model->getProfiles()[m_currentProfileIndex];
315 QString profileDirectory = profile.getDirectory();
317 QDesktopServices::openUrl(QUrl::fromLocalFile(profileDirectory));
320 int CProfilesDialog::getIndexFromProfileLanguage(const QString &lang) const
322 if (lang == "en") return 0;
323 if (lang == "fr") return 1;
324 if (lang == "de") return 2;
325 if (lang == "es") return 3;
326 if (lang == "ru") return 4;
328 return -1;
331 QString CProfilesDialog::getProfileLanguageFromIndex(int index) const
333 if (index == 0) return "en";
334 if (index == 1) return "fr";
335 if (index == 2) return "de";
336 if (index == 3) return "es";
337 if (index == 4) return "ru";
338 // locale
339 return CConfigFile::getInstance()->getLanguage();