1 | /* This file is part of the KDE project |
2 | Copyright (C) 2000, 2007 David Faure <faure@kde.org> |
3 | |
4 | This program is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU General Public |
6 | License version 2 or at your option version 3 as published by |
7 | the Free Software Foundation. |
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 GNU |
12 | 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; see the file COPYING. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | // Own |
21 | #include "keditfiletype.h" |
22 | #include "mimetypewriter.h" |
23 | |
24 | // Qt |
25 | #include <qdbusconnection.h> |
26 | #include <qdbusmessage.h> |
27 | #include <QtCore/QFile> |
28 | |
29 | // KDE |
30 | #include <kapplication.h> |
31 | #include <kaboutdata.h> |
32 | #include <kbuildsycocaprogressdialog.h> |
33 | #include <kdebug.h> |
34 | #include <kcmdlineargs.h> |
35 | #include <ksycoca.h> |
36 | #include <kservicetypeprofile.h> |
37 | #include <kstandarddirs.h> |
38 | #include <klocale.h> |
39 | #include <kwindowsystem.h> |
40 | |
41 | // Local |
42 | #include "filetypedetails.h" |
43 | #include "typeslistitem.h" |
44 | |
45 | |
46 | FileTypeDialog::FileTypeDialog( MimeTypeData* mime ) |
47 | : KDialog( 0 ), |
48 | m_mimeTypeData(mime) |
49 | { |
50 | setButtons( Cancel | Apply | Ok ); |
51 | |
52 | init(); |
53 | } |
54 | |
55 | FileTypeDialog::~FileTypeDialog() |
56 | { |
57 | delete m_details; |
58 | } |
59 | |
60 | void FileTypeDialog::init() |
61 | { |
62 | m_details = new FileTypeDetails( this ); |
63 | m_details->setMimeTypeData( m_mimeTypeData ); |
64 | |
65 | // This code is very similar to kcdialog.cpp |
66 | setMainWidget( m_details ); |
67 | connect(m_details, SIGNAL(changed(bool)), this, SLOT(clientChanged(bool))); |
68 | // TODO setHelp() |
69 | enableButton(Apply, false); |
70 | |
71 | connect(KSycoca::self(), SIGNAL(databaseChanged(QStringList)), SLOT(slotDatabaseChanged(QStringList))); |
72 | connect( this, SIGNAL( okClicked() ), SLOT( slotOk() ) ); |
73 | connect( this, SIGNAL( applyClicked() ), SLOT( slotApply() ) ); |
74 | } |
75 | |
76 | void FileTypeDialog::save() |
77 | { |
78 | if (m_mimeTypeData->isDirty()) { |
79 | const bool servicesDirty = m_mimeTypeData->isServiceListDirty(); |
80 | if (m_mimeTypeData->sync()) |
81 | MimeTypeWriter::runUpdateMimeDatabase(); |
82 | if (servicesDirty) |
83 | KBuildSycocaProgressDialog::rebuildKSycoca(this); |
84 | // Trigger reparseConfiguration of filetypesrc in konqueror |
85 | QDBusMessage message = |
86 | QDBusMessage::createSignal("/KonqMain" , "org.kde.Konqueror.Main" , "reparseConfiguration" ); |
87 | QDBusConnection::sessionBus().send(message); |
88 | } |
89 | } |
90 | |
91 | void FileTypeDialog::slotOk() |
92 | { |
93 | save(); |
94 | accept(); |
95 | } |
96 | |
97 | void FileTypeDialog::slotApply() |
98 | { |
99 | save(); |
100 | } |
101 | |
102 | void FileTypeDialog::clientChanged(bool state) |
103 | { |
104 | // enable/disable buttons |
105 | enableButton(User1, state); |
106 | enableButton(Apply, state); |
107 | } |
108 | |
109 | void FileTypeDialog::slotDatabaseChanged(const QStringList& changedResources) |
110 | { |
111 | kDebug() << changedResources; |
112 | if ( changedResources.contains("xdgdata-mime" ) // changes in mimetype definitions |
113 | || changedResources.contains("services" ) ) { // changes in .desktop files |
114 | m_details->refresh(); |
115 | } |
116 | } |
117 | |
118 | #include "keditfiletype.moc" |
119 | |
120 | int main(int argc, char ** argv) |
121 | { |
122 | KServiceTypeProfile::setConfigurationMode(); |
123 | KAboutData aboutData( "keditfiletype" , "filetypes" , ki18n("KEditFileType" ), "1.0" , |
124 | ki18n("KDE file type editor - simplified version for editing a single file type" ), |
125 | KAboutData::License_GPL, |
126 | ki18n("(c) 2000, KDE developers" ) ); |
127 | aboutData.addAuthor(ki18n("Preston Brown" ),KLocalizedString(), "pbrown@kde.org" ); |
128 | aboutData.addAuthor(ki18n("David Faure" ),KLocalizedString(), "faure@kde.org" ); |
129 | aboutData.setProgramIconName("preferences-desktop-filetype-association" ); |
130 | |
131 | KCmdLineArgs::init( argc, argv, &aboutData ); |
132 | |
133 | KCmdLineOptions options; |
134 | options.add("parent <winid>" , ki18n("Makes the dialog transient for the window specified by winid" )); |
135 | options.add("+mimetype" , ki18n("File type to edit (e.g. text/html)" )); |
136 | KCmdLineArgs::addCmdLineOptions( options ); // Add our own options. |
137 | KApplication app; |
138 | KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); |
139 | |
140 | if (args->count() == 0) |
141 | KCmdLineArgs::usage(); |
142 | |
143 | QString arg = args->arg(0); |
144 | MimeTypeData* mimeTypeData = 0; |
145 | const bool createType = arg.startsWith('*'); |
146 | if ( createType ) { |
147 | QString mimeString = "application/x-kdeuser%1" ; |
148 | QString mimeTypeName; |
149 | int inc = 0; |
150 | bool ok = false; |
151 | do { |
152 | ++inc; |
153 | mimeTypeName = mimeString.arg(inc); |
154 | ok = !KMimeType::mimeType(mimeTypeName); |
155 | } while (!ok); |
156 | |
157 | QStringList patterns; |
158 | if ( arg.length() > 2 ) |
159 | patterns << arg.toLower() << arg.toUpper(); |
160 | QString ; |
161 | if ( arg.startsWith( QLatin1String("*." ) ) && arg.length() >= 3 ) { |
162 | const QString type = arg.mid( 3 ).prepend( arg[2].toUpper() ); |
163 | comment = i18n( "%1 File" , type ); |
164 | } |
165 | |
166 | mimeTypeData = new MimeTypeData(mimeTypeName, true); // new mimetype |
167 | mimeTypeData->setComment(comment); |
168 | mimeTypeData->setPatterns(patterns); |
169 | } |
170 | else { |
171 | const QString mimeTypeName = arg; |
172 | KMimeType::Ptr mime = KMimeType::mimeType(mimeTypeName, KMimeType::ResolveAliases); |
173 | if (!mime) { |
174 | kError() << "Mimetype" << mimeTypeName << "not found" ; |
175 | return 1; |
176 | } |
177 | |
178 | mimeTypeData = new MimeTypeData(mime); |
179 | } |
180 | |
181 | FileTypeDialog dlg( mimeTypeData ); |
182 | if( args->isSet( "parent" )) { |
183 | bool ok; |
184 | long id = QString(args->getOption("parent" )).toLong(&ok); |
185 | if (ok) |
186 | KWindowSystem::setMainWindow( &dlg, (WId)id ); |
187 | } |
188 | args->clear(); |
189 | if ( !createType ) |
190 | dlg.setCaption( i18n("Edit File Type %1" , mimeTypeData->name()) ); |
191 | else { |
192 | dlg.setCaption( i18n("Create New File Type %1" , mimeTypeData->name()) ); |
193 | dlg.enableButton( KDialog::Apply, true ); |
194 | } |
195 | |
196 | dlg.show(); // non-modal |
197 | |
198 | return app.exec(); |
199 | } |
200 | |
201 | |