mirror of
https://bitbucket.org/smil3y/kde-workspace.git
synced 2025-02-23 18:32:50 +00:00
kfile: reimplement it
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
4b3759716c
commit
f9c449dcd1
5 changed files with 101 additions and 370 deletions
|
@ -1,7 +1,9 @@
|
||||||
set(kfile_SRCS fileprops.cpp )
|
set(kfile_SRCS kfile.cpp)
|
||||||
|
|
||||||
add_executable(kfile4 ${kfile_SRCS})
|
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}
|
||||||
|
)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
This is a commandline frontend to KFileMetaInfo. It allows
|
This is a commandline frontend to KFileMetaInfo. It allows to read meta
|
||||||
to read and write meta information of files.
|
information of URL(s).
|
||||||
|
|
||||||
Carsten Pfeiffer <pfeiffer@kde.org>
|
Ivailo Monev <xakepa10@gmail.com>
|
||||||
|
|
|
@ -1,307 +0,0 @@
|
||||||
/* This file is part of the KDE libraries
|
|
||||||
Copyright (C) 2002,2003 Carsten Pfeiffer <pfeiffer@kde.org>
|
|
||||||
|
|
||||||
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 <iostream>
|
|
||||||
|
|
||||||
#include <QtCore/QFile>
|
|
||||||
|
|
||||||
#include <kaboutdata.h>
|
|
||||||
#include <kapplication.h>
|
|
||||||
#include <kcmdlineargs.h>
|
|
||||||
#include <kfilemetainfo.h>
|
|
||||||
#include <klocale.h>
|
|
||||||
#include <kpropertiesdialog.h>
|
|
||||||
#include <kdebug.h>
|
|
||||||
|
|
||||||
#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<FileProps *> 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 <key>", ki18n("Prints the value for 'key' of the given file(s). 'key' "
|
|
||||||
"may also be a comma-separated list of keys"));
|
|
||||||
options.add("setValue <key=value>", 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<FileProps *> 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;
|
|
||||||
}
|
|
|
@ -1,57 +0,0 @@
|
||||||
/* This file is part of the KDE libraries
|
|
||||||
Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
|
|
||||||
|
|
||||||
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 <QtCore/QString>
|
|
||||||
|
|
||||||
#include <kfilemetainfo.h>
|
|
||||||
#include <kurl.h>
|
|
||||||
|
|
||||||
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
|
|
93
kfile/kfile.cpp
Normal file
93
kfile/kfile.cpp
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
/* This file is part of the KDE project
|
||||||
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
||||||
|
|
||||||
|
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 <kaboutdata.h>
|
||||||
|
#include <kapplication.h>
|
||||||
|
#include <kcmdlineargs.h>
|
||||||
|
#include <kfilemetainfo.h>
|
||||||
|
#include <kurl.h>
|
||||||
|
#include <klocale.h>
|
||||||
|
#include <kdebug.h>
|
||||||
|
|
||||||
|
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 <key>", 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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue