From d6a4fe9ca855e094bbfe2e1ba1bba565557b1891 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sat, 31 Dec 2011 17:36:26 -0600 Subject: [PATCH] Add tdelfeditor --- CMakeLists.txt | 3 + tdecore/kapplication.cpp | 3 +- tdelfeditor/CMakeLists.txt | 33 +++ tdelfeditor/tdelfeditor.cpp | 561 ++++++++++++++++++++++++++++++++++++ tdelfeditor/tdelfeditor.h | 40 +++ 5 files changed, 639 insertions(+), 1 deletion(-) create mode 100644 tdelfeditor/CMakeLists.txt create mode 100644 tdelfeditor/tdelfeditor.cpp create mode 100644 tdelfeditor/tdelfeditor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e8a6902c..35e54560c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -800,6 +800,9 @@ add_subdirectory( libltdl ) add_subdirectory( kglib ) add_subdirectory( tdefx ) add_subdirectory( tdecore ) +if( WITH_ELFICON ) + add_subdirectory( tdelfeditor ) +endif( WITH_ELFICON ) add_subdirectory( kunittest ) add_subdirectory( tdeui ) add_subdirectory( tdesu ) diff --git a/tdecore/kapplication.cpp b/tdecore/kapplication.cpp index 6e5ae5d40..38ee6190a 100644 --- a/tdecore/kapplication.cpp +++ b/tdecore/kapplication.cpp @@ -646,7 +646,8 @@ KApplication::KApplication( int& argc, char** argv, const TQCString& rAppName, } KApplication::KApplication( bool allowStyles, bool GUIenabled ) : - TQApplication( *KCmdLineArgs::qt_argc(), *KCmdLineArgs::qt_argv(), TRUE ), // Qt4 requires that there always be a GUI +// TQApplication( *KCmdLineArgs::qt_argc(), *KCmdLineArgs::qt_argv(), TRUE ), // Qt4 requires that there always be a GUI + TQApplication( *KCmdLineArgs::qt_argc(), *KCmdLineArgs::qt_argv(), GUIenabled ), // We need to be able to run command line apps KInstance( KCmdLineArgs::about), #ifdef Q_WS_X11 display(0L), diff --git a/tdelfeditor/CMakeLists.txt b/tdelfeditor/CMakeLists.txt new file mode 100644 index 000000000..c18421cef --- /dev/null +++ b/tdelfeditor/CMakeLists.txt @@ -0,0 +1,33 @@ +################################################# +# +# (C) 2011 Timothy Pearson +# kb9vqf (AT) pearsoncomputing.net +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${TQT_INCLUDE_DIRS} + ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR}/tdecore + ${CMAKE_SOURCE_DIR}/dcop + ${CMAKE_SOURCE_DIR}/tdecore + ${LIBR_INCLUDEDIR} +) + +link_directories( + ${TQT_LIBRARY_DIRS} + ${LIBR_LIBDIR} +) + + +##### tdelfeditor ################################ + +tde_add_executable( tdelfeditor + SOURCES tdelfeditor.cpp + DESTINATION ${BIN_INSTALL_DIR} + LINK ${TQT_LIBRARIES} ${LIBR_LIBRARIES} tdecore-shared +) diff --git a/tdelfeditor/tdelfeditor.cpp b/tdelfeditor/tdelfeditor.cpp new file mode 100644 index 000000000..14de9d973 --- /dev/null +++ b/tdelfeditor/tdelfeditor.cpp @@ -0,0 +1,561 @@ +/* + * + * Copyright (c) 2008-2011 Erich E. Hoover + * TDE version (c) 2011 Timothy Pearson + * + * elfres - Adds resources into ELF files + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * To provide feedback, report bugs, or otherwise contact me: + * ehoover at mines dot edu + * + */ + +/* Obtaining command-line options */ +#include + +/* Internationalization support */ +extern "C" { + #include +} + +#include "tdelfeditor.h" + +#include +#include + +#include +#include +#include +#include +#include +#include + +/* return application name */ +extern const char *__progname; +#define progname() __progname + +typedef enum { + PARAM_PROGNAME = 0, + PARAM_OPTIONS = 1, + PARAM_ELF_FILE = 2, + PARAM_UUID = 3, + PARAM_RESOURCE_NAME = 3, + PARAM_ICON_NAME = 3, + PARAM_ICON_SIZE = 3, + PARAM_RESOURCE_FILE = 4, + PARAM_ICON_FILE = 4, +} eParams; + +typedef struct { + char short_options[ELFICON_OPTIONS+1]; + struct option long_options[ELFICON_OPTIONS+1]; + char descriptions[ELFICON_OPTIONS][100]; +} IconOptions; + +IconOptions elficon_options = { + "acefgrstv", + { + {"add-icon", 0, 0, 'a'}, + {"clear-icons", 0, 0, 'c'}, + {"set-empty-uuid", 0, 0, 'e'}, + {"find-icon", 0, 0, 'f'}, + {"get-uuid", 0, 0, 'g'}, + {"retrieve-icon", 0, 0, 'r'}, + {"set-uuid", 0, 0, 's'}, + {"tde-autoadd-icon", 0, 0, 't'}, + {"version", 0, 0, 'v'}, + {NULL, 0, 0, 0} + }, + { + N_("add an icon to the ELF file"), + N_("clear the file's ELF icon"), + N_("set an empty icon UUID for the ELF file"), + N_("find an ELF icon in the file by closest size"), + N_("get the icon UUID for the file"), + N_("retrieve an icon from the ELF file"), + N_("set the icon UUID for the ELF file"), + N_("automatically add the appropriate TDE icon to the ELF file"), + N_("display the current application revision"), + } +}; + +typedef struct { + char short_options[ELFRES_OPTIONS+1]; + struct option long_options[ELFRES_OPTIONS+1]; + char descriptions[ELFRES_OPTIONS][100]; +} RcOptions; + +RcOptions elfres_options = { + "aclrv", + { + {"add", 0, 0, 'a'}, + {"clear", 0, 0, 'c'}, + {"list", 0, 0, 'l'}, + {"retrieve", 0, 0, 'r'}, + {"version", 0, 0, 'v'}, + {NULL, 0, 0, 0} + }, + { + N_("add a resource to the ELF file"), + N_("clear an ELF file resource"), + N_("list libr-compatible resources"), + N_("retrieve a resource from the ELF file"), + N_("display the current application revision"), + } +}; + +typedef enum { + MODE_ADD_RESOURCE, + MODE_CLEAR_RESOURCE, + MODE_RETRIEVE_RESOURCE, + MODE_LIST_RESOURCES, + MODE_ADD_ICON, + MODE_TDE_AUTOADD_ICON, + MODE_RETRIEVE_ICON, + MODE_CLEAR_ICON, + MODE_SET_UUID, + MODE_SET_EMPTY_UUID, + MODE_GET_UUID, + MODE_FIND_ICON, + MODE_LAUNCH_GUI +} eMode; + +/* + * Return the size of the file represented by the file handle + */ +off_t file_size(FILE *file) +{ + struct stat file_stat; + + if(fstat(fileno(file), &file_stat) == ERROR) + return ERROR; + return file_stat.st_size; +} + +/* + * Output the application version + */ +void output_version(char *progname) +{ + printf(_("%s: Version %s\n"), progname, VERSION); +} + +/* + * Use command-line arguments to set the appropriate data mode + */ +int handle_arguments(int argc, char **argv, eMode *mode) +{ + int required_params = 0; + int i, c, index = 0; + + opterr = 0; /* Prevent automatic getopt() error message */ + + while ((c = getopt_long(argc, argv, elficon_options.short_options, elficon_options.long_options, &index)) != EOF) + { + switch(c) + { + case 'a': + *mode = MODE_ADD_ICON; + required_params = 5; + break; + case 'c': + *mode = MODE_CLEAR_ICON; + required_params = 3; + break; + case 'e': + *mode = MODE_SET_EMPTY_UUID; + required_params = 3; + break; + case 'f': + *mode = MODE_FIND_ICON; + required_params = 5; + break; + case 'g': + *mode = MODE_GET_UUID; + required_params = 3; + break; + case 'r': + *mode = MODE_RETRIEVE_ICON; + required_params = 5; + break; + case 's': + *mode = MODE_SET_UUID; + required_params = 4; + break; + case 't': + *mode = MODE_TDE_AUTOADD_ICON; + required_params = 4; + break; + case 'v': + output_version(argv[PARAM_PROGNAME]); + return FALSE; + default: + goto print_icon_usage; + } + index++; + if(argc != required_params) + goto print_icon_usage; + } + return TRUE; +print_icon_usage: + fprintf(stderr, _("usage: %s [-a|-r] elf-file-name icon-name svg-file-name\n"), argv[PARAM_PROGNAME]); + fprintf(stderr, _("usage: %s [-t] elf-file-name icon-name\n"), argv[PARAM_PROGNAME]); + fprintf(stderr, _("usage: %s [-c|-e|-g] elf-file-name\n"), argv[PARAM_PROGNAME]); + fprintf(stderr, _("usage: %s [-s] elf-file-name uuid\n"), argv[PARAM_PROGNAME]); + fprintf(stderr, _("If -t is set the TDEDIRS environment variable must include your TDE installation prefix\n")); + fprintf(stderr, _("for example: TDEDIRS=/opt/trinity ./tdelfeditor -t ./konqueror konqueror\n")); + for(i=0;iresourceDirs("icon"); + for ( TQStringList::Iterator it = rds.begin(); it != rds.end(); ++it ) { + printf(" * %s\n\r", (*it).ascii()); fflush(stdout); + } + TQString systemIcon = KGlobal::iconLoader()->iconPath(argv[PARAM_ICON_NAME], 0, true); + if (systemIcon.isNull()) { + systemIcon = KGlobal::iconLoader()->iconPath(argv[PARAM_ICON_NAME], 0, false); + printf("NOT FOUND, using %s\n\r", systemIcon.ascii()); + } + else { + printf("found %s\n\r", systemIcon.ascii()); + } + + libr_icon *icon = NULL; + + icon = libr_icon_newicon_byfile(LIBR_SVG, 0, const_cast(systemIcon.ascii())); + if(icon == NULL) + { + errorf(_("failed to open icon file \"%s\": %s"), systemIcon.ascii(), libr_errmsg()); + goto fail; + } + if(!libr_icon_write(handle, icon, const_cast(systemIcon.ascii()), LIBR_OVERWRITE)) + { + libr_icon_close(icon); + errorf(_("failed to add icon to ELF file: %s"), libr_errmsg()); + goto fail; + } + libr_icon_close(icon); + } break; + case MODE_ADD_RESOURCE: + if(section == NULL) + { + section = argv[PARAM_RESOURCE_NAME]; + input_file = argv[PARAM_RESOURCE_FILE]; + } + add_resource(handle, section, input_file); + break; + case MODE_RETRIEVE_ICON: + { + libr_icon *icon = NULL; + + icon = libr_icon_geticon_byname(handle, argv[PARAM_ICON_NAME]); + if(icon == NULL) + { + errorf(_("failed to get icon named \"%s\": %s"), argv[PARAM_ICON_NAME], libr_errmsg()); + goto fail; + } + if(!libr_icon_save(icon, argv[PARAM_ICON_FILE])) + { + libr_icon_close(icon); + errorf(_("failed to save the icon to a file: %s"), libr_errmsg()); + goto fail; + } + libr_icon_close(icon); + } break; + case MODE_RETRIEVE_RESOURCE: + if(section == NULL) + { + section = argv[PARAM_RESOURCE_NAME]; + output_file = argv[PARAM_RESOURCE_FILE]; + } + get_resource(handle, section, output_file); + break; + default: + errorf(_("Unhandled operation code.")); + goto fail; + } + +fail: + /* Close file handle and exit */ + libr_close(handle); + return EX_OK; +} + +/* + * Main execution routine + */ +int main(int argc, char **argv) +{ + libr_i18n_autoload(PACKAGE); + return main_console(argc, argv); +} diff --git a/tdelfeditor/tdelfeditor.h b/tdelfeditor/tdelfeditor.h new file mode 100644 index 000000000..6cfdeddfc --- /dev/null +++ b/tdelfeditor/tdelfeditor.h @@ -0,0 +1,40 @@ +#ifndef __ELFRES_H +#define __ELFRES_H + +#define VERSION "0.0.1" +#define PACKAGE "elfres" + +/* Handle ELF Resources */ +extern "C" { + #include + #include +} + +/* Handle strings */ +#include + +/* Obtain file information */ +#include +#include +#include + +/* Handle exit cases and errors */ +#include + +#ifndef FALSE + #define FALSE 0 +#endif +#ifndef TRUE + #define TRUE 1 +#endif + +#define ERROR -1 +#define ERROR_BUF 1024 +#define ELFICON_OPTIONS 9 +#define ICON_SECTION ".icon" +#define con_err(...) fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n") +#define errorf(...) con_err(__VA_ARGS__) + +#define ELFRES_OPTIONS 5 + +#endif /* __ELFRES_H */