1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
18 #include "uninstalldialog.h"
19 #include "configfile.h"
22 #include "nel/misc/system_info.h"
23 #include "nel/misc/common.h"
29 CUninstallDialog::CUninstallDialog(QWidget
*parent
):QDialog(parent
), m_installerIndex(-1)
31 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint
);
35 CConfigFile
*config
= CConfigFile::getInstance();
37 int serverCount
= config
->getServersCount();
39 QStandardItemModel
*model
= new QStandardItemModel(0, 2, this);
42 columns
<< tr("Component");
43 columns
<< tr("Size");
45 model
->setHorizontalHeaderLabels(columns
);
47 QStandardItem
*item
= NULL
;
50 for (int row
= 0; row
< serverCount
; ++row
)
52 const CServer
&server
= config
->getServer(row
);
54 if (QFile::exists(server
.getDirectory()))
56 m_serversIndices
[server
.id
] = model
->rowCount();
58 item
= new QStandardItem(tr("Client for %1").arg(server
.name
));
59 item
->setCheckable(true);
60 model
->appendRow(item
);
64 int profilesCount
= config
->getProfilesCount();
67 for (int row
= 0; row
< profilesCount
; ++row
)
69 const CProfile
&profile
= config
->getProfile(row
);
71 m_profilesIndices
[profile
.id
] = model
->rowCount();
73 item
= new QStandardItem(tr("Profile #%1: %2").arg(profile
.id
).arg(profile
.name
));
74 item
->setCheckable(true);
75 model
->appendRow(item
);
80 m_installerIndex
= model
->rowCount();
82 item
= new QStandardItem(tr("Installer"));
83 item
->setCheckable(true);
84 model
->appendRow(item
);
87 m_downloadedFilesIndex
= model
->rowCount();
89 item
= new QStandardItem(tr("Downloaded Files"));
90 item
->setCheckable(true);
91 model
->appendRow(item
);
93 componentsTreeView
->setModel(model
);
94 componentsTreeView
->resizeColumnToContents(0);
96 // resize layout depending on content and constraints
99 // fix height because to left bitmap
100 setFixedHeight(height());
103 connect(uninstallButton
, SIGNAL(clicked()), SLOT(accept()));
104 connect(cancelButton
, SIGNAL(clicked()), SLOT(reject()));
105 connect(model
, SIGNAL(itemChanged(QStandardItem
*)), SLOT(onItemChanged(QStandardItem
*)));
107 // semi-hack to not update UI on another thread
108 connect(this, SIGNAL(updateSize(int, QString
)), SLOT(onUpdateSize(int, QString
)));
109 connect(this, SIGNAL(updateLayout()), SLOT(onUpdateLayout()));
112 CUninstallDialog::~CUninstallDialog()
116 void CUninstallDialog::showEvent(QShowEvent
*event
)
118 QDialog::showEvent(event
);
120 // update size of all components sizes in a thread to not block interface
121 QtConcurrent::run(this, &CUninstallDialog::updateSizes
);
124 void CUninstallDialog::setSelectedComponents(const SComponents
&components
)
126 QStandardItemModel
*model
= qobject_cast
<QStandardItemModel
*>(componentsTreeView
->model());
127 if (model
== NULL
) return;
129 QStandardItem
*item
= NULL
;
132 IDIndicesMap::const_iterator it
= m_serversIndices
.begin(), iend
= m_serversIndices
.end();
136 item
= model
->item(it
.value());
138 if (item
) item
->setCheckState(components
.servers
.indexOf(it
.key()) > -1 ? Qt::Checked
: Qt::Unchecked
);
144 it
= m_profilesIndices
.begin(), iend
= m_profilesIndices
.end();
148 item
= model
->item(it
.value());
150 if (item
) item
->setCheckState(components
.profiles
.indexOf(it
.key()) > -1 ? Qt::Checked
: Qt::Unchecked
);
156 item
= model
->item(m_installerIndex
);
157 if (item
) item
->setCheckState(components
.installer
? Qt::Checked
: Qt::Unchecked
);
160 item
= model
->item(m_downloadedFilesIndex
);
161 if (item
) item
->setCheckState(components
.downloadedFiles
? Qt::Checked
: Qt::Unchecked
);
164 SComponents
CUninstallDialog::getSelectedCompenents() const
168 QStandardItemModel
*model
= qobject_cast
<QStandardItemModel
*>(componentsTreeView
->model());
169 if (model
== NULL
) return res
;
171 QStandardItem
*item
= NULL
;
174 IDIndicesMap::const_iterator it
= m_serversIndices
.begin(), iend
= m_serversIndices
.end();
178 item
= model
->item(it
.value());
180 if (item
&& item
->checkState() == Qt::Checked
) res
.servers
<< it
.key();
186 it
= m_profilesIndices
.begin(), iend
= m_profilesIndices
.end();
190 item
= model
->item(it
.value());
192 if (item
&& item
->checkState() == Qt::Checked
) res
.profiles
<< it
.key();
198 item
= model
->item(m_installerIndex
);
199 res
.installer
= item
&& item
->checkState() == Qt::Checked
;
202 item
= model
->item(m_downloadedFilesIndex
);
203 res
.downloadedFiles
= item
&& item
->checkState() == Qt::Checked
;
208 void CUninstallDialog::accept()
213 void CUninstallDialog::onItemChanged(QStandardItem
* /* item */)
218 void CUninstallDialog::onUpdateSize(int row
, const QString
&text
)
220 QStandardItemModel
*model
= qobject_cast
<QStandardItemModel
*>(componentsTreeView
->model());
221 if (model
== NULL
) return;
223 // set size for a component
224 QStandardItem
*item
= new QStandardItem(text
);
225 model
->setItem(row
, 1, item
);
228 void CUninstallDialog::onUpdateLayout()
230 componentsTreeView
->resizeColumnToContents(1);
235 void CUninstallDialog::updateSizes()
237 CConfigFile
*config
= CConfigFile::getInstance();
240 IDIndicesMap::const_iterator it
= m_serversIndices
.begin(), iend
= m_serversIndices
.end();
244 const CServer
&server
= config
->getServer(it
.key());
246 qint64 bytes
= getDirectorySize(server
.getDirectory(), true);
248 emit
updateSize(it
.value(), qBytesToHumanReadable(bytes
));
254 it
= m_profilesIndices
.begin(), iend
= m_profilesIndices
.end();
258 const CProfile
&profile
= config
->getProfile(it
.key());
261 if (profile
.id
.isEmpty()) continue;
263 qint64 bytes
= getDirectorySize(profile
.getDirectory(), true);
265 emit
updateSize(it
.value(), qBytesToHumanReadable(bytes
));
273 QDir
dir(config
->getInstallationDirectory());
283 QFileInfoList downloadedFiles
= dir
.entryInfoList(filters
, QDir::Files
);
285 foreach(const QFileInfo
&info
, downloadedFiles
)
287 bytes
+= info
.size();
290 emit
updateSize(m_downloadedFilesIndex
, qBytesToHumanReadable(bytes
));
295 void CUninstallDialog::updateButtons()
297 QStandardItemModel
*model
= qobject_cast
<QStandardItemModel
*>(componentsTreeView
->model());
298 if (model
== NULL
) return;
300 int checkedCount
= 0;
302 for (int i
= 0; i
< model
->rowCount(); ++i
)
304 if (model
->item(i
)->checkState() == Qt::Checked
) ++checkedCount
;
307 // Uninstall button should be enabled only if at least one component is checked
308 uninstallButton
->setEnabled(checkedCount
> 0);