1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2008-2009 Licq developers
4  *
5  * Licq 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; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "config.h"
21 
22 #include "filenameedit.h"
23 
24 #ifdef USE_KDE
25 # include <KDE/KFileDialog>
26 #else
27 #include <QFileDialog>
28 #include <QLineEdit>
29 #include <QHBoxLayout>
30 #include <QToolButton>
31 #endif
32 
33 using namespace LicqQtGui;
34 /* TRANSLATOR LicqQtGui::FileNameEdit */
35 
FileNameEdit(QWidget * parent)36 FileNameEdit::FileNameEdit(QWidget* parent)
37   : FILENAMEEDIT_BASE(parent)
38 {
39 #ifdef USE_KDE
40   connect(this, SIGNAL(openFileDialog(KUrlRequester*)), SLOT(dialogAboutToOpen()));
41 #else
42   QHBoxLayout* lay = new QHBoxLayout(this);
43   lay->setContentsMargins(0, 0, 0, 0);
44 
45   // Input field
46   editField = new QLineEdit();
47   lay->addWidget(editField);
48 
49   // Button to open file dialog
50   QToolButton* browseButton = new QToolButton();
51   // TODO: Use an open file icon instead of text for browseButton
52   browseButton->setText(tr("Browse..."));
53   connect(browseButton, SIGNAL(clicked()), SLOT(browse()));
54   lay->addWidget(browseButton);
55 #endif
56 }
57 
setFileName(const QString & fileName)58 void FileNameEdit::setFileName(const QString& fileName)
59 {
60 #ifdef USE_KDE
61   setUrl(KUrl(fileName));
62 #else
63   editField->setText(fileName);
64 #endif
65 }
66 
fileName() const67 QString FileNameEdit::fileName() const
68 {
69 #ifdef USE_KDE
70   return url().pathOrUrl();
71 #else
72   return editField->text();
73 #endif
74 }
75 
76 #ifdef USE_KDE
dialogAboutToOpen()77 void FileNameEdit::dialogAboutToOpen()
78 {
79   // Set the default path for dialog to start in
80   if (url().pathOrUrl().isEmpty() && !myDefaultPath.isEmpty())
81     fileDialog()->setUrl(KUrl(myDefaultPath));
82 }
83 #else
setFilter(const QString & filter)84 void FileNameEdit::setFilter(const QString& filter)
85 {
86   myFilter = filter;
87 
88   // Convert from KFileDialog fiter to QFileDialog filter syntax
89 
90   // Remove suffixes before pipes (i.e. "*.txt|Text files (*.txt)" => "Text files (*.txt)"
91   myFilter.replace(QRegExp("[^\\n\\|]*\\|"), "");
92 
93   // Remove escaping backslash before slash
94   myFilter.replace("\\/", "/");
95 
96   // Replace line breaks (\n) with two semicolons (;;).
97   myFilter.replace("\n", ";;");
98 }
99 
browse()100 void FileNameEdit::browse()
101 {
102   QString filename = editField->text();
103   if (filename.isEmpty())
104     filename = myDefaultPath;
105 
106   filename = QFileDialog::getOpenFileName(this, QString(), filename, myFilter);
107 
108   if (filename.isNull())
109     return;
110 
111   editField->setText(filename);
112 }
113 #endif
114