add more spacing
[personal-kdebase.git] / runtime / kmimetypefinder / kmimetypefinder.cpp
blob26124bbd4d85fdb361f8996bc33e92261286b745
1 /*
2 * Copyright (C) 2002 David Faure <faure@kde.org>
3 * Copyright (C) 2008 Pino Toscano <pino@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation;
9 * This library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include <QFile>
22 #include <kmimetype.h>
23 #include <kcmdlineargs.h>
24 //#include <kapplication.h>
25 #include <kdeversion.h>
26 #include <kcomponentdata.h>
28 #include <stdio.h>
30 int main(int argc, char *argv[])
32 KCmdLineArgs::init( argc, argv, "kmimetypefinder", 0, ki18n("MimeType Finder"), KDE_VERSION_STRING , ki18n("Gives the mimetype for a given file"));
35 KCmdLineOptions options;
37 options.add("c").add("content", ki18n("Use only the file content for determining the mimetype."));
38 options.add("f").add("filename-only", ki18n("Whether use the file name only for determining the mimetype. Not used if -c is specified."));
39 options.add("+filename", ki18n("The filename to test. '-' to read from stdin."));
41 KCmdLineArgs::addCmdLineOptions( options );
43 // KApplication app;
44 KComponentData instance("kmimetypefinder");
46 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
47 if( args->count() < 1 ) {
48 printf( "No filename specified\n" );
49 return 1;
51 const QString fileName = args->arg( 0 );
52 int accuracy;
53 KMimeType::Ptr mime;
54 if (fileName == QLatin1String("-")) {
55 QFile qstdin;
56 qstdin.open(stdin, QIODevice::ReadOnly);
57 const QByteArray data = qstdin.readAll();
58 mime = KMimeType::findByContent(data, &accuracy);
59 } else if (args->isSet("c")) {
60 mime = KMimeType::findByFileContent(fileName, &accuracy);
61 } else {
62 mime = KMimeType::findByPath(fileName, 0, args->isSet("f"), &accuracy);
64 if ( mime && mime->name() != KMimeType::defaultMimeType() ) {
65 printf("%s\n", mime->name().toLatin1().constData());
66 printf("(accuracy %d)\n", accuracy);
67 } else {
68 return 1; // error
71 return 0;