/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "backupmanagerform.h"
#include "ui_backupmanagerform.h"
#include "cmaobject.h"
#include "sforeader.h"
#include "confirmdialog.h"
#include "utils.h"
#include
#include
#include
#include
#include
BackupManagerForm::BackupManagerForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::BackupManagerForm)
{
ui->setupUi(this);
setupForm();
}
BackupManagerForm::~BackupManagerForm()
{
delete ui;
}
void BackupManagerForm::setupForm()
{
this->resize(800, 480);
connect(ui->backupComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(loadBackupListing(int)));
ui->tableWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
ui->tableWidget->horizontalHeader()->hide();
// the the account name when vitamtp returns this value
ui->accountBox->addItem(QSettings().value("lastAccountId", tr("Default account")).toString());
}
void BackupManagerForm::removeEntry(BackupItem *item)
{
ConfirmDialog msgBox;
msgBox.setMessageText(tr("Are you sure to remove the backup of the following entry?"), item->title);
msgBox.setMessagePixmap(*item->getIconPixmap(), item->getIconWidth());
switch(msgBox.exec()) {
case QDialogButtonBox::Ok:
break;
case QDialogButtonBox::Cancel:
return;
default:
return;
}
QMutexLocker locker(&db->mutex);
CMAObject *obj = db->ohfiToObject(item->ohfi);
if(obj) {
obj->removeReferencedObject();
db->remove(obj);
}
ui->tableWidget->removeRow(item->row);
}
void BackupManagerForm::loadBackupListing(int index)
{
int ohfi;
bool sys_dir;
int img_width;
ui->tableWidget->clear();
switch(index) {
case 0:
ohfi = VITA_OHFI_VITAAPP;
img_width = 48;
sys_dir = true;
break;
case 1:
ohfi = VITA_OHFI_PSPAPP;
img_width = 80;
sys_dir = true;
break;
case 2:
ohfi = VITA_OHFI_PSMAPP;
img_width = 48;
sys_dir = true;
break;
case 3:
ohfi = VITA_OHFI_PSXAPP;
img_width = 48;
sys_dir = true;
break;
case 4:
ohfi = VITA_OHFI_PSPSAVE;
img_width = 80;
sys_dir = false;
break;
case 5:
ohfi = VITA_OHFI_BACKUP;
img_width = 48;
sys_dir = false;
break;
default:
ohfi = VITA_OHFI_VITAAPP;
img_width = 48;
sys_dir = true;
}
db->mutex.lock();
// get the item list
metadata_t *meta;
int row_count = db->filterObjects(ohfi, &meta);
ui->tableWidget->setRowCount(row_count);
// adjust the table item width to fill all the widget
QHeaderView *vert_header = ui->tableWidget->verticalHeader();
QHeaderView *horiz_header = ui->tableWidget->horizontalHeader();
horiz_header->setResizeMode(QHeaderView::Stretch);
CMAObject *obj = db->ohfiToObject(ohfi);
ui->usageLabel->setText(tr("Backup disk usage: %1").arg(readable_size(obj->metadata.size, true)));
QList item_list;
while(meta) {
QString base_path = obj->path + QDir::separator() + meta->name;
QString parent_path = sys_dir ? base_path + QDir::separator() + "sce_sys" : base_path;
SfoReader reader;
QString game_name;
// retrieve the game name from the SFO
if(reader.load(QDir(parent_path).absoluteFilePath(sys_dir ? "param.sfo" : "PARAM.SFO"))) {
game_name = QString::fromUtf8(reader.value("TITLE", meta->name));
} else {
game_name = QString(meta->name);
}
BackupItem *item = new BackupItem();
// save the game title and ohfi for sorting/deleting
item->ohfi = meta->ohfi;
item->title = game_name;
connect(item, SIGNAL(deleteEntry(BackupItem*)), this, SLOT(removeEntry(BackupItem*)));
QString size = readable_size(meta->size);
// check if the game data is present, else is just a LiveArea launcher
if(sys_dir && !(QDir(base_path + QDir::separator() + "app").exists() || QDir(base_path + QDir::separator() + "game").exists())) {
size.append(tr(" - (Launcher only)"));
}
item->setItemInfo(game_name, size);
item->setItemIcon(QDir(parent_path).absoluteFilePath(sys_dir ? "icon0.png" : "ICON0.PNG"), img_width);
item->setDirectory(obj->path + QDir::separator() + meta->name);
item->resize(646, 60);
item_list << item;
meta = meta->next_metadata;
}
qSort(item_list.begin(), item_list.end(), BackupItem::lessThan);
int row;
QList::iterator it;
vert_header->setUpdatesEnabled(false);
// insert the sorted items into the table
for(it = item_list.begin(), row = 0; it != item_list.end(); ++it, ++row) {
(*it)->row = row;
ui->tableWidget->setCellWidget(row, 0, *it);
vert_header->resizeSection(row, 60);
}
vert_header->setUpdatesEnabled(true);
db->mutex.unlock();
}