summaryrefslogtreecommitdiff
path: root/plugins/CopyEngine/Ultracopier-0.3/fileIsSameDialog.cpp
blob: a618f4c7198412c5adbdbcd569454633a66356b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "fileIsSameDialog.h"
#include "ui_fileIsSameDialog.h"

#include <QDebug>

fileIsSameDialog::fileIsSameDialog(QWidget *parent,QFileInfo fileInfo) :
	QDialog(parent),
	ui(new Ui::fileIsSameDialog)
{
	ui->setupUi(this);
	action=FileExists_Cancel;
	oldName=fileInfo.fileName();
	destinationInfo=fileInfo;
	ui->lineEditNewName->setText(oldName);
	ui->lineEditNewName->setPlaceholderText(oldName);
	ui->label_content_size->setText(QString::number(fileInfo.size()));
	ui->label_content_modified->setText(fileInfo.lastModified().toString());
	ui->label_content_file_name->setText(fileInfo.fileName());
	updateRenameButton();
	QDateTime maxTime(QDate(ULTRACOPIER_PLUGIN_MINIMALYEAR,1,1));
	if(maxTime<fileInfo.lastModified())
	{
		ui->label_modified->setVisible(true);
		ui->label_content_modified->setVisible(true);
		ui->label_content_modified->setText(fileInfo.lastModified().toString());
	}
	else
	{
		ui->label_modified->setVisible(false);
		ui->label_content_modified->setVisible(false);
	}
}

fileIsSameDialog::~fileIsSameDialog()
{
	delete ui;
}

void fileIsSameDialog::changeEvent(QEvent *e)
{
	QWidget::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		ui->retranslateUi(this);
		break;
	default:
		break;
	}
}

QString fileIsSameDialog::getNewName()
{
	if(oldName==ui->lineEditNewName->text() || ui->checkBoxAlways->isChecked())
		qDebug() << "fileIsSameDialog, return the old name: "+oldName;
	else
		qDebug() << "fileIsSameDialog, return the new name: "+ui->lineEditNewName->text();
	if(oldName==ui->lineEditNewName->text() || ui->checkBoxAlways->isChecked())
		return oldName;
	else
		return ui->lineEditNewName->text();
}

void fileIsSameDialog::on_SuggestNewName_clicked()
{
	QFileInfo destinationInfo=this->destinationInfo;
	QString absolutePath=destinationInfo.absolutePath();
	QString fileName=destinationInfo.fileName();
	QString suffix="";
	QString destination;
	if(fileName.contains(QRegExp("^(.*)(\\.[a-z0-9]+)$")))
	{
		suffix=fileName;
		suffix.replace(QRegExp("^(.*)(\\.[a-z0-9]+)$"),"\\2");
		fileName.replace(QRegExp("^(.*)(\\.[a-z0-9]+)$"),"\\1");
	}
	do
	{
		if(!fileName.startsWith(tr("Copy of ")))
			fileName=tr("Copy of ")+fileName;
		else
		{
			if(fileName.contains(QRegExp("_[0-9]+$")))
			{
				QString number=fileName;
				number.replace(QRegExp("^.*_([0-9]+)$"),"\\1");
				int num=number.toInt()+1;
				fileName.remove(QRegExp("[0-9]+$"));
				fileName+=QString::number(num);
			}
			else
				fileName+="_2";
		}
		destination=absolutePath+QDir::separator()+fileName+suffix;
		destinationInfo.setFile(destination);
	}
	while(destinationInfo.exists());
	ui->lineEditNewName->setText(fileName+suffix);
}

void fileIsSameDialog::on_Rename_clicked()
{
	action=FileExists_Rename;
	this->close();
}

void fileIsSameDialog::on_Skip_clicked()
{
	action=FileExists_Skip;
	this->close();
}

void fileIsSameDialog::on_Cancel_clicked()
{
	action=FileExists_Cancel;
	this->close();
}

FileExistsAction fileIsSameDialog::getAction()
{
	return action;
}

bool fileIsSameDialog::getAlways()
{
	return ui->checkBoxAlways->isChecked();
}

void fileIsSameDialog::updateRenameButton()
{
	ui->Rename->setEnabled(ui->checkBoxAlways->isChecked() || (oldName!=ui->lineEditNewName->text() && !ui->lineEditNewName->text().isEmpty()));
}

void fileIsSameDialog::on_lineEditNewName_textChanged(const QString &arg1)
{
	Q_UNUSED(arg1);
	updateRenameButton();
}

void fileIsSameDialog::on_checkBoxAlways_toggled(bool checked)
{
	Q_UNUSED(checked);
	updateRenameButton();
}