1 //=============================================================================
2 //
3 //   File : KviFileDialog.cpp
4 //   Creation date : Mon Nov 20 2000 12:20:42 CEST by Szymon Stefanek
5 //
6 //   This file is part of the KVIrc IRC client distribution
7 //   Copyright (C) 2000-2010 Szymon Stefanek (pragma at kvirc dot net)
8 //
9 //   This program is FREE software. You can redistribute it and/or
10 //   modify it under the terms of the GNU General Public License
11 //   as published by the Free Software Foundation; either version 2
12 //   of the License, or (at your option) any later version.
13 //
14 //   This program is distributed in the HOPE that it will be USEFUL,
15 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 //   See the GNU General Public License for more details.
18 //
19 //   You should have received a copy of the GNU General Public License
20 //   along with this program. If not, write to the Free Software Foundation,
21 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=============================================================================
24 
25 #include "KviFileDialog.h"
26 #include "KviMediaManager.h"
27 #include "KviIconManager.h"
28 #include "KviLocale.h"
29 #include "KviApplication.h"
30 #include "KviFileUtils.h"
31 #include "KviMainWindow.h"
32 #include "KviTalToolTip.h"
33 
34 #include <QDir>
35 #include <QIcon>
36 #include <QMessageBox>
37 #include <QFileDialog>
38 
39 extern KviMediaManager * g_pMediaManager;
40 
KviFileDialog(const QString & szDirName,const QString & szFilter,QWidget * pParent,const char * name,bool bModal)41 KviFileDialog::KviFileDialog(const QString & szDirName, const QString & szFilter, QWidget * pParent, const char * name, bool bModal)
42     : KviTalFileDialog(szDirName, szFilter, pParent, name, bModal)
43 {
44 	setWindowIcon(QIcon(*(g_pIconManager->getSmallIcon(KviIconManager::Folder))));
45 }
46 
47 KviFileDialog::~KviFileDialog()
48     = default;
49 
50 /*
51 void KviFileDialog::goKvirc()
52 {
53 	KviCString tmp;
54 	g_pApp->getLocalKvircDirectory(tmp,KviApplication::None);
55 	setDir(QDir(tmp.ptr()));
56 }
57 
58 void KviFileDialog::goHome()
59 {
60 	setDir(QDir::home());
61 }
62 */
63 
64 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
askForOpenFileName(QString & szBuffer,const QString & szCaption,const QString & szInitial,const QString & szFilter,bool,bool bShowNative,QWidget * pParent)65 bool KviFileDialog::askForOpenFileName(QString & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool bShowNative, QWidget * pParent)
66 {
67 	if(bShowNative)
68 	{
69 		szBuffer = QFileDialog::getOpenFileName(pParent, szCaption, szInitial, szFilter);
70 		KviFileUtils::adjustFilePath(szBuffer);
71 		return !szBuffer.isEmpty();
72 	}
73 #else
74 bool KviFileDialog::askForOpenFileName(QString & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool, QWidget * pParent)
75 {
76 #endif
77 	KviFileDialog * pDialog = new KviFileDialog(szInitial, szFilter, pParent, "open_file_name_dialog", true);
78 	pDialog->setWindowTitle(szCaption);
79 	// i was ignorant
80 	pDialog->setFileMode(KviTalFileDialog::ExistingFile);
81 	//pDialog->setShowHiddenFiles(showHidden);
82 
83 	if(pDialog->exec() == QDialog::Accepted)
84 	{
85 		QStringList szFiles = pDialog->selectedFiles();
86 		if(!szFiles.isEmpty())
87 			szBuffer = szFiles[0];
88 		KviFileUtils::adjustFilePath(szBuffer);
89 		delete pDialog;
90 		return !szBuffer.isEmpty();
91 	}
92 	delete pDialog;
93 	return false;
94 }
95 
96 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
97 bool KviFileDialog::askForSaveFileName(QString & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool bConfirmOverwrite, bool bShowNative, QWidget * pParent)
98 {
99 	if(bShowNative)
100 	{
101 		while(1)
102 		{
103 			szBuffer = QFileDialog::getSaveFileName(pParent, szCaption, szInitial, szFilter, 0, QFileDialog::DontConfirmOverwrite);
104 			KviFileUtils::adjustFilePath(szBuffer);
105 			//return !buffer.isEmpty();
106 			if(szBuffer.isEmpty())
107 				return false;
108 
109 			if(!bConfirmOverwrite)
110 				return true;
111 
112 			// Check for the file existence
113 			if(!KviFileUtils::fileExists(szBuffer))
114 				return true;
115 
116 			QString szTmp = QString(__tr2qs("The file %1 already exists.<br>Do you wish to overwrite it?")).arg(szBuffer);
117 
118 			switch(QMessageBox::information(pParent, __tr2qs("File Already Exists - KVIrc"), szTmp, QMessageBox::Yes, QMessageBox::No | QMessageBox::Default, QMessageBox::Cancel | QMessageBox::Escape))
119 			{
120 				case QMessageBox::Cancel:
121 					return false;
122 					break;
123 				case QMessageBox::Yes:
124 					return true;
125 					break;
126 			}
127 		}
128 	}
129 #else
130 bool KviFileDialog::askForSaveFileName(QString & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool bConfirmOverwrite, bool, QWidget * pParent)
131 {
132 #endif
133 
134 	KviFileDialog * pDialog = new KviFileDialog(szInitial, szFilter, pParent, "save_file_name_dialog", true);
135 	pDialog->setWindowTitle(szCaption);
136 	// 190
137 	pDialog->setFileMode(KviTalFileDialog::AnyFile);
138 //pDialog->setShowHiddenFiles(showHidden);
139 #ifdef COMPILE_KDE4_SUPPORT
140 	pDialog->setKeepLocation(true);
141 #endif
142 	while(pDialog->exec() == QDialog::Accepted)
143 	{
144 		QStringList szFiles = pDialog->selectedFiles();
145 		if(!szFiles.isEmpty())
146 			szBuffer = szFiles[0];
147 		KviFileUtils::adjustFilePath(szBuffer);
148 
149 		if(!szBuffer.isEmpty())
150 		{
151 			if(!bConfirmOverwrite)
152 			{
153 				delete pDialog;
154 				return true;
155 			}
156 
157 			// Check for the file existence
158 			if(KviFileUtils::fileExists(szBuffer))
159 			{
160 				QString szTmp = QString(__tr2qs("The file %1 already exists.<br>Do you wish to overwrite it?")).arg(szBuffer);
161 
162 				switch(QMessageBox::information(pDialog, __tr2qs("File Already Exists - KVIrc"), szTmp, QMessageBox::Yes, QMessageBox::No | QMessageBox::Default, QMessageBox::Cancel | QMessageBox::Escape))
163 				{
164 					case QMessageBox::Cancel:
165 						delete pDialog;
166 						return false;
167 						break;
168 					case QMessageBox::Yes:
169 						delete pDialog;
170 						return true;
171 						break;
172 				}
173 			}
174 			else
175 			{
176 				delete pDialog;
177 				return true; // ok...file not exists
178 			}
179 		}
180 		else
181 		{
182 			delete pDialog;
183 			return false; // empty buffer
184 		}
185 	}
186 
187 	delete pDialog;
188 	return false;
189 }
190 
191 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
192 bool KviFileDialog::askForDirectoryName(QString & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool bShowNative, QWidget * pParent)
193 {
194 	if(bShowNative)
195 	{
196 		szBuffer = QFileDialog::getExistingDirectory(pParent, szCaption, szInitial);
197 		KviFileUtils::adjustFilePath(szBuffer);
198 		return !szBuffer.isEmpty();
199 	}
200 #else
201 bool KviFileDialog::askForDirectoryName(QString & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool, QWidget * pParent)
202 {
203 #ifdef COMPILE_KDE4_SUPPORT
204 	// the KDE based dir selection dialog is now quite nice
205 	szBuffer = KFileDialog::getExistingDirectory(szInitial, pParent, szCaption);
206 	return !szBuffer.isEmpty();
207 #endif
208 #endif
209 
210 	KviFileDialog * pDialog = new KviFileDialog(szInitial,
211 	    szFilter, pParent, "directory_name_dialog", true);
212 	pDialog->setWindowTitle(szCaption);
213 	// Move to tal and settle matters there?
214 	pDialog->setFileMode(KviTalFileDialog::Directory);
215 	//pDialog->setShowHiddenFiles(showHidden);
216 
217 	if(pDialog->exec() == QDialog::Accepted)
218 	{
219 		QStringList szFiles = pDialog->selectedFiles();
220 		if(!szFiles.isEmpty())
221 			szBuffer = szFiles[0];
222 		KviFileUtils::adjustFilePath(szBuffer);
223 		delete pDialog;
224 		return !szBuffer.isEmpty();
225 	}
226 	delete pDialog;
227 
228 	return false;
229 }
230 
231 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
232 bool KviFileDialog::askForOpenFileNames(QStringList & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool bShowNative, QWidget * pParent)
233 {
234 	if(bShowNative)
235 	{
236 		szBuffer = QFileDialog::getOpenFileNames(pParent, szCaption, szInitial, szFilter);
237 		return (szBuffer.count() > 0);
238 	}
239 #else
240 bool KviFileDialog::askForOpenFileNames(QStringList & szBuffer, const QString & szCaption, const QString & szInitial, const QString & szFilter, bool, bool, QWidget * pParent)
241 {
242 #endif
243 
244 	KviFileDialog * pDialog = new KviFileDialog(szInitial, szFilter, pParent, "open_file_names_dialog", true);
245 	pDialog->setWindowTitle(szCaption);
246 	// See line 190
247 	pDialog->setFileMode(KviTalFileDialog::ExistingFiles);
248 	//pDialog->setShowHiddenFiles(showHidden);
249 
250 	if(pDialog->exec() == QDialog::Accepted)
251 	{
252 		szBuffer = pDialog->selectedFiles();
253 		delete pDialog;
254 		return (szBuffer.count() > 0);
255 	}
256 	delete pDialog;
257 	return false;
258 }
259