1 /*
2  * Hedgewars, a free turn based strategy game
3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License
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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include "pageplayrecord.h"
20 
21 #include <QFont>
22 #include <QGridLayout>
23 #include <QPushButton>
24 #include <QListWidget>
25 #include <QListWidgetItem>
26 #include <QFileInfo>
27 #include <QMessageBox>
28 #include <QInputDialog>
29 
30 #include "hwconsts.h"
31 
32 #include "DataManager.h"
33 
bodyLayoutDefinition()34 QLayout * PagePlayDemo::bodyLayoutDefinition()
35 {
36     QGridLayout * pageLayout = new QGridLayout();
37 
38     pageLayout->setColumnStretch(0, 1);
39     pageLayout->setColumnStretch(1, 2);
40     pageLayout->setColumnStretch(2, 1);
41     pageLayout->setRowStretch(2, 100);
42 
43     BtnRenameRecord = new QPushButton(this);
44     BtnRenameRecord->setText(QPushButton::tr("Rename"));
45     pageLayout->addWidget(BtnRenameRecord, 0, 2);
46 
47     BtnRemoveRecord = new QPushButton(this);
48     BtnRemoveRecord->setText(QPushButton::tr("Delete"));
49     pageLayout->addWidget(BtnRemoveRecord, 1, 2);
50 
51     DemosList = new QListWidget(this);
52     DemosList->setGeometry(QRect(170, 10, 311, 311));
53     pageLayout->addWidget(DemosList, 0, 1, 3, 1);
54 
55     return pageLayout;
56 }
57 
footerLayoutDefinition()58 QLayout * PagePlayDemo::footerLayoutDefinition()
59 {
60     QHBoxLayout * bottomLayout = new QHBoxLayout();
61 
62     BtnPlayDemo = addButton(tr("Play demo"), bottomLayout, 0, false, Qt::AlignBottom);
63     const QIcon& lp = QIcon(":/res/Start.png");
64     QSize sz = lp.actualSize(QSize(65535, 65535));
65     BtnPlayDemo->setStyleSheet("padding: 5px 10px");
66     BtnPlayDemo->setIcon(lp);
67     BtnPlayDemo->setFixedHeight(50);
68     BtnPlayDemo->setIconSize(sz);
69     BtnPlayDemo->setFlat(true);
70     BtnPlayDemo->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
71 
72     return bottomLayout;
73 }
74 
connectSignals()75 void PagePlayDemo::connectSignals()
76 {
77     connect(BtnRenameRecord, SIGNAL(clicked()), this, SLOT(renameRecord()));
78     connect(BtnRemoveRecord, SIGNAL(clicked()), this, SLOT(removeRecord()));
79     connect(&DataManager::instance(), SIGNAL(updated()), this, SLOT(refresh()));
80 }
81 
PagePlayDemo(QWidget * parent)82 PagePlayDemo::PagePlayDemo(QWidget* parent) : AbstractPage(parent)
83 {
84     initPage();
85 }
86 
FillFromDir(RecordType rectype)87 void PagePlayDemo::FillFromDir(RecordType rectype)
88 {
89     QDir dir;
90     QString extension;
91 
92     recType = rectype;
93 
94     dir.cd(cfgdir->absolutePath());
95     if (rectype == RT_Demo)
96     {
97         dir.cd("Demos");
98         extension = "hwd";
99         BtnPlayDemo->setText(QPushButton::tr("Play demo"));
100         BtnPlayDemo->setWhatsThis(tr("Play the selected demo"));
101     }
102     else
103     {
104         dir.cd("Saves");
105         extension = "hws";
106         BtnPlayDemo->setText(QPushButton::tr("Load"));
107         BtnPlayDemo->setWhatsThis(tr("Load the selected game"));
108     }
109     dir.setFilter(QDir::Files);
110 
111     QStringList sl = dir.entryList(QStringList(QString("*.%2.%1").arg(extension, *cProtoVer)));
112     sl.replaceInStrings(QRegExp(QString("^(.*)\\.%2\\.%1$").arg(extension, *cProtoVer)), "\\1");
113 
114     DemosList->clear();
115     DemosList->addItems(sl);
116 
117     for (int i = 0; i < DemosList->count(); ++i)
118     {
119         DemosList->item(i)->setData(Qt::UserRole, dir.absoluteFilePath(QString("%1.%3.%2").arg(sl[i], extension, *cProtoVer)));
120         DemosList->item(i)->setIcon(recType == RT_Demo ? QIcon(":/res/file_demo.png") : QIcon(":/res/file_save.png"));
121     }
122 }
123 
124 
refresh()125 void PagePlayDemo::refresh()
126 {
127     if (this->isVisible())
128         FillFromDir(recType);
129 }
130 
131 
renameRecord()132 void PagePlayDemo::renameRecord()
133 {
134     QListWidgetItem * curritem = DemosList->currentItem();
135     if (!curritem)
136     {
137         QMessageBox recordMsg(this);
138         recordMsg.setIcon(QMessageBox::Warning);
139         recordMsg.setWindowTitle(QMessageBox::tr("Error"));
140         recordMsg.setText(QMessageBox::tr("Please select a file from the list."));
141         recordMsg.setTextFormat(Qt::PlainText);
142         recordMsg.setWindowModality(Qt::WindowModal);
143         recordMsg.exec();
144         return ;
145     }
146     QFile rfile(curritem->data(Qt::UserRole).toString());
147 
148     QFileInfo finfo(rfile);
149 
150     bool ok;
151 
152     QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName().replace("." + *cProtoVer, ""), &ok);
153 
154     if(ok && newname.size())
155     {
156         QString newfullname = QString("%1/%2.%3.%4")
157                               .arg(finfo.absolutePath())
158                               .arg(newname)
159                               .arg(*cProtoVer)
160                               .arg(finfo.suffix());
161 
162         ok = rfile.rename(newfullname);
163         if(!ok)
164         {
165             QMessageBox renameMsg(this);
166             renameMsg.setIcon(QMessageBox::Warning);
167             renameMsg.setWindowTitle(QMessageBox::tr("Error"));
168             renameMsg.setText(QMessageBox::tr("Cannot rename file to %1.").arg(newfullname));
169             renameMsg.setTextFormat(Qt::PlainText);
170             renameMsg.setWindowModality(Qt::WindowModal);
171             renameMsg.exec();
172         }
173         else
174             FillFromDir(recType);
175     }
176 }
177 
removeRecord()178 void PagePlayDemo::removeRecord()
179 {
180     QListWidgetItem * curritem = DemosList->currentItem();
181     if (!curritem)
182     {
183         QMessageBox recordMsg(this);
184         recordMsg.setIcon(QMessageBox::Warning);
185         recordMsg.setWindowTitle(QMessageBox::tr("Error"));
186         recordMsg.setText(QMessageBox::tr("Please select a file from the list."));
187         recordMsg.setTextFormat(Qt::PlainText);
188         recordMsg.setWindowModality(Qt::WindowModal);
189         recordMsg.exec();
190         return ;
191     }
192     QFile rfile(curritem->data(Qt::UserRole).toString());
193 
194     bool ok;
195 
196     ok = rfile.remove();
197     if(!ok)
198     {
199         QMessageBox removeMsg(this);
200         removeMsg.setIcon(QMessageBox::Warning);
201         removeMsg.setWindowTitle(QMessageBox::tr("Error"));
202         removeMsg.setText(QMessageBox::tr("Cannot delete file %1.").arg(rfile.fileName()));
203         removeMsg.setTextFormat(Qt::PlainText);
204         removeMsg.setWindowModality(Qt::WindowModal);
205         removeMsg.exec();
206     }
207     else
208     {
209         int i = DemosList->row(curritem);
210         delete curritem;
211         DemosList->setCurrentRow(i < DemosList->count() ? i : DemosList->count() - 1);
212     }
213 }
214 
isSave()215 bool PagePlayDemo::isSave()
216 {
217     return recType == RT_Save;
218 }
219