diff --git a/kfile/CMakeLists.txt b/kfile/CMakeLists.txt index 89b16fb3..72089127 100644 --- a/kfile/CMakeLists.txt +++ b/kfile/CMakeLists.txt @@ -1,7 +1,9 @@ -set(kfile_SRCS fileprops.cpp ) +set(kfile_SRCS kfile.cpp) add_executable(kfile4 ${kfile_SRCS}) -target_link_libraries(kfile4 ${KDE4_KIO_LIBS} ) +target_link_libraries(kfile4 ${KDE4_KIO_LIBS}) -install(TARGETS kfile4 ${INSTALL_TARGETS_DEFAULT_ARGS} ) +install( + TARGETS kfile4 ${INSTALL_TARGETS_DEFAULT_ARGS} +) diff --git a/kfile/README b/kfile/README index d063b6d5..d3c6f257 100644 --- a/kfile/README +++ b/kfile/README @@ -1,4 +1,4 @@ -This is a commandline frontend to KFileMetaInfo. It allows -to read and write meta information of files. +This is a commandline frontend to KFileMetaInfo. It allows to read meta +information of URL(s). -Carsten Pfeiffer +Ivailo Monev diff --git a/kfile/fileprops.cpp b/kfile/fileprops.cpp deleted file mode 100644 index e18c0a67..00000000 --- a/kfile/fileprops.cpp +++ /dev/null @@ -1,307 +0,0 @@ -/* This file is part of the KDE libraries - Copyright (C) 2002,2003 Carsten Pfeiffer - - library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation, version 2. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "fileprops.h" - -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -#define KFILEVERSION "0.2" -#define INDENT "\t" - -using namespace std; - -static QString beatifyValue( const QString& value ) -{ - if ( value.isNull() ) - return QString("(no value for key available)"); - else if ( value.isEmpty() ) - return QString("(empty)"); - - return value; -} - -FileProps::FileProps( const QString& path ) - : m_dirty( false ) -{ - m_info = new KFileMetaInfo(path, KFileMetaInfo::Everything); -} - -FileProps::~FileProps() -{ - sync(); - delete m_info; -} - -bool FileProps::sync() -{ - if ( !m_dirty ) - return true; - - return m_info->applyChanges(); -} - -bool FileProps::isValid() const -{ - return m_info->isValid(); -} - -QStringList FileProps::supportedKeys() const -{ - return QStringList(); -} - -QStringList FileProps::availableKeys() const -{ - return m_info->keys(); -} - -QString FileProps::getValue( const QString& key ) const -{ - return FileProps::createKeyValue( m_info->item(key) ); -} - -bool FileProps::setValue( const QString& key, const QString &value ) -{ - bool ok = m_info->item(key).setValue( value ); - m_dirty |= ok; - return ok; -} - -QStringList FileProps::allValues() const -{ - return FileProps::createKeyValueList( m_info->items().values() ); -} - -// static helper: -// creates strings like -// "group: translatedKey: value" -QString FileProps::createKeyValue( const KFileMetaInfoItem& item ) -{ - static const int MAX_SPACE = 25; - - QString result("%1"); - result = result.arg(item.name() + ':', -MAX_SPACE ); - result.append( beatifyValue( item.value().toString() ) ); - - return result; -} - -// static -QStringList FileProps::createKeyValueList( const KFileMetaInfoItemList& items ) -{ - QStringList result; - KFileMetaInfoItemList::ConstIterator it = items.begin(); - - for ( ; it != items.end(); ++it ) - result.append( FileProps::createKeyValue( *it ) ); - - return result; -} - -/////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////// - - - -// kfile --mimetype --listsupported --listavailable --listwritable --getValue "key" --setValue "key=value" --allValues --dialog --quiet file [file...] -// "key" may be a list of keys, separated by commas - -// -// helper functions -// - -// Caller needs to delete the items in the list after use! -static KFileItemList fileItemList( const KCmdLineArgs *args ) -{ - KFileItemList items; - for ( int i = 0; i < args->count(); i++ ) - items.append( KFileItem( KFileItem::Unknown, - KFileItem::Unknown, - args->url( i ) )); - return items; -} - -static void showPropertiesDialog( const KCmdLineArgs *args ) { - const KFileItemList items = fileItemList( args ); - KPropertiesDialog::showDialog( items, 0, true ); -} -/* -static void printMimeTypes( const KCmdLineArgs *args ) -{ - for ( int i = 0; i < args->count(); i++ ) - { - KUrl url = args->url( i ); - KMimeType::Ptr mt = KMimeType::findByUrl( url ); - kDebug() << args->arg(i) << ": " << mt->comment().toLocal8Bit() << " (" - << mt->name().toLocal8Bit() << ")" << endl; - } -}*/ - -static void printList( const QStringList& list ) -{ - QStringList::ConstIterator it = list.begin(); - for ( ; it != list.end(); ++it ) - kDebug() << (*it).toLocal8Bit(); - kDebug(); -} - -static void processMetaDataOptions( const QList propList, - KCmdLineArgs *args ) -{ -// kfile --mimetype --listsupported --listavailable --listwritable --getValue "key" --setValue "key=value" --allValues --dialog --quiet file [file...] -// "key" may be a list of keys, separated by commas - - QString line("-- -------------------------------------------------------"); - foreach ( FileProps *props, propList ) - { - QString file = props->fileName() + ' '; - QString fileString = line; - fileString.replace( 3, file.length(), file ); - kDebug() << QFile::encodeName( fileString ); - - if ( args->isSet( "listsupported" ) ) - { - kDebug() << "=Supported Keys="; - printList( props->supportedKeys() ); - } - if ( args->isSet( "listavailable" ) ) - { - kDebug() << "=Available Keys="; - printList( props->availableKeys() ); - } -// if ( args->isSet( "listwritable" ) ) -// { -// kDebug() << "TODO :)"; -// } - if ( args->isSet( "getValue" ) ) - { - kDebug() << "=Value="; - QString key = args->getOption("getValue"); - kDebug() << props->getValue( key ).toLocal8Bit(); - } - - if ( args->isSet( "setValue" ) ) - { - // separate key and value from the line "key=value" - QString cmd = args->getOption("setValue"); - QString key = cmd.section( '=', 0, 0 ); - QString value = cmd.section( '=', 1 ); - - props->setValue(key, value); - } - - if ( args->isSet( "allValues" ) ) - { - kDebug() << "=All Values="; - printList( props->allValues() ); - } - } - -} - -int main( int argc, char **argv ) -{ - KAboutData about( - "kfile", 0, ki18n( "kfile" ), KFILEVERSION, - ki18n("A command-line tool to read and modify metadata of files."), - KAboutData::License_LGPL, ki18n("(c) 2002, Carsten Pfeiffer"), - ki18n(0 /*text*/), "http://devel-home.kde.org/~pfeiffer/", - "pfeiffer@kde.org" ); - - about.addAuthor( ki18n("Carsten Pfeiffer"), KLocalizedString(), "pfeiffer@kde.org", - "http://devel-home.kde.org/~pfeiffer/" ); - - KCmdLineArgs::init( argc, argv, &about ); - - - KCmdLineOptions options; - - options.add("m"); // short option for --mimetype - options.add("nomimetype", ki18n("Do not print the mimetype of the given file(s)")); - options.add("ls"); // short option for --listsupported - options.add("listsupported", ki18n("List all supported metadata keys." )); - options.add("la"); // short option for --listavailable - options.add("listavailable", ki18n("List all metadata keys which have a value in the given " - "file(s).")); - options.add("q"); // short option for --quiet - options.add("quiet", ki18n("Do not print a warning when more than one file was given " - "and they do not all have the same mimetype.")); - options.add("av"); // short option for --allValues - options.add("allValues", ki18n("Prints all metadata values, available in the given " - "file(s).")); - options.add("dialog", ki18n("Opens a KDE properties dialog to allow viewing and " - "modifying of metadata of the given file(s)")); - options.add("getValue ", ki18n("Prints the value for 'key' of the given file(s). 'key' " - "may also be a comma-separated list of keys")); - options.add("setValue ", ki18n("Attempts to set the value 'value' for the metadata key " - "'key' for the given file(s)")); - options.add("+[files]", ki18n("The file (or a number of files) to operate on.")); - KCmdLineArgs::addCmdLineOptions( options ); - - KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); - - QList m_props; - - bool quiet = args->isSet( "quiet" ); - - int files = args->count(); - if ( files == 0 ) { - KCmdLineArgs::usageError( i18n("No files specified") ); // exit()s - } - - if ( args->isSet( "dialog" ) ) { - KApplication app; - showPropertiesDialog( args ); - return 0; - } - - QString mimeType; - - for ( int i = 0; i < files; i++ ) { - //if ( args->isSet( "mimetype" ) ) - //printMimeTypes( args ); - - FileProps *props = new FileProps( args->url(i).path()); - if ( props->isValid() ) { - m_props.append( props ); - } else { - if ( !quiet ) { - kWarning() << args->arg(i) << ": " << - i18n("Cannot determine metadata").toLocal8Bit() << endl; - } - delete props; - } - } - - - processMetaDataOptions( m_props, args ); - - qDeleteAll(m_props); // force destruction/sync of props - - return 0; -} diff --git a/kfile/fileprops.h b/kfile/fileprops.h deleted file mode 100644 index c8f4e260..00000000 --- a/kfile/fileprops.h +++ /dev/null @@ -1,57 +0,0 @@ -/* This file is part of the KDE libraries - Copyright (C) 2002 Carsten Pfeiffer - - library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation, version 2. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#ifndef KFILEPROPS_H -#define KFILEPROPS_H - -#include - -#include -#include - -class FileProps -{ -public: - FileProps( const QString& path); - virtual ~FileProps(); - - bool isValid() const; - - QString fileName() const { return m_info->url().path(); } - - QStringList supportedKeys() const; - QStringList availableKeys() const; - - QString getValue( const QString& key ) const; - bool setValue( const QString& key, const QString &value ); - - QStringList allValues() const; - - bool isReadOnly( const QString& key ); - -private: - static QString createKeyValue( const KFileMetaInfoItem& item ); - static QStringList createKeyValueList( const KFileMetaInfoItemList& items); - bool sync(); - - KFileMetaInfo *m_info; - bool m_dirty; - -}; - -#endif // KFILEPROPS_H diff --git a/kfile/kfile.cpp b/kfile/kfile.cpp new file mode 100644 index 00000000..d359069f --- /dev/null +++ b/kfile/kfile.cpp @@ -0,0 +1,93 @@ +/* This file is part of the KDE project + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + KAboutData aboutData("kfile", 0, ki18n("kfile4"), + "1.0.0", ki18n("A command-line tool to read metadata of files."), + KAboutData::License_GPL_V2, + ki18n("(c) 2022 Ivailo Monev"), + KLocalizedString(), + "http://github.com/fluxer/katana" + ); + aboutData.addAuthor(ki18n("Ivailo Monev"), + ki18n("Maintainer"), + "xakepa10@gmail.com"); + + KCmdLineArgs::init(argc, argv, &aboutData); + + KCmdLineOptions options; + options.add("ls"); // short option for --listsupported + options.add("listsupported", ki18n("List all supported metadata keys." )); + options.add("la"); // short option for --listavailable + options.add("listavailable", ki18n("List all metadata keys which have a value in the given URL(s).")); + options.add("av"); // short option for --allValues + options.add("allvalues", ki18n("Prints all metadata values, available in the given URL(s).")); + options.add("getvalue ", ki18n("Prints the value for 'key' of the given URL(s). 'key' may also be a comma-separated list of keys")); + options.add("+[url]", ki18n("The URL(s) to operate on.")); + KCmdLineArgs::addCmdLineOptions( options ); + + KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); + + if (args->count() == 0) { + // exits + KCmdLineArgs::usageError(i18n("No URL specified")); + } + + const bool listsupported = (args->isSet("ls") || args->isSet("listsupported")); + const bool listavailable = (args->isSet("la") || args->isSet("listavailable")); + const bool allvalues = (args->isSet("av") || args->isSet("allvalues")); + const bool getvalue = args->isSet("getvalue"); + if (!listsupported && !listavailable && !allvalues && !getvalue) { + KCmdLineArgs::usageError(i18n("One of listsupported, listavailable, allvalues or getvalue must be specified")); + } + + for (int pos = 0; pos < args->count(); ++pos) { + const KUrl url = args->url(pos); + const KFileMetaInfo metainfo(url, KFileMetaInfo::Everything); + if (listsupported) { + foreach (const QString &key, metainfo.supportedKeys()) { + qDebug() << key; + } + } else if (listavailable) { + foreach (const QString &key, metainfo.keys()) { + qDebug() << key; + } + } else if (allvalues) { + foreach (const KFileMetaInfoItem &item, metainfo.items().values()) { + qDebug() << item.key() << item.value().toString(); + } + } else if (getvalue) { + const QString getvaluekey = args->getOption("getvalue"); + const KFileMetaInfoItem item = metainfo.item(getvaluekey); + qDebug() << item.value().toString(); + } else { + Q_ASSERT(false); + } + } + + return 0; +}