diff --git a/tools/bookmark.c b/tools/bookmark.c new file mode 100644 index 0000000..55029b4 --- /dev/null +++ b/tools/bookmark.c @@ -0,0 +1,152 @@ +/* $Id$ */ +/* Copyright (c) 2015 Pierre Pronchery */ +/* This file is part of DeforaOS Desktop Surfer */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + + + +#include +#include +#include +#include +#include +#include +#include +#include "../config.h" +#define _(string) gettext(string) + +/* constants */ +#ifndef PROGNAME +# define PROGNAME "bookmark" +#endif +#ifndef PREFIX +# define PREFIX "/usr/local" +#endif +#ifndef DATADIR +# define DATADIR PREFIX "/share" +#endif +#ifndef LOCALEDIR +# define LOCALEDIR DATADIR "/locale" +#endif + + +/* bookmark */ +/* private */ +/* prototypes */ +static int _bookmark(char const * title, char const * url, char const * icon, + char const * comment); + +static int _error(char const * message, int ret); +static int _usage(void); + + +/* functions */ +/* bookmark */ +static int _bookmark(char const * title, char const * url, char const * icon, + char const * comment) +{ + int ret; + const char section[] = "Desktop Entry"; + char const * homedir; + String * filename = NULL; + Config * config; + String * p; + + if((homedir = getenv("HOME")) == NULL) + homedir = g_get_home_dir(); + if(title == NULL) + title = url; + if((p = string_new(title)) == NULL) + { + error_print(PROGNAME); + return -1; + } + string_replace(&p, "/", "_"); + if((filename = string_new_append(homedir, "/.local/share/applications/", + p, ".desktop", NULL)) == NULL + || (config = config_new()) == NULL) + { + string_delete(filename); + string_delete(p); + error_print(PROGNAME); + return -1; + } + if((ret = config_set(config, section, "Type", "URL")) != 0 + || (ret = config_set(config, section, "Name", title)) + || (ret = config_set(config, section, "URL", url)) != 0 + || (ret = config_set(config, section, "Icon", icon)) + || (ret = config_set(config, section, "Comment", + comment)) != 0 + || (ret = config_save(config, filename)) != 0) + error_print(PROGNAME); + config_delete(config); + string_delete(filename); + string_delete(p); + return ret; +} + + +/* error */ +static int _error(char const * message, int ret) +{ + fputs(PROGNAME ": ", stderr); + perror(message); + return ret; +} + + +/* usage */ +static int _usage(void) +{ + fprintf(stderr, _("Usage: %s -u [-t title][-i icon][-C comment] URL\n"), + PROGNAME); + return 1; +} + + +/* public */ +/* main */ +int main(int argc, char * argv[]) +{ + int o; + int url = 0; + char const * title = NULL; + char const * icon = "stock_internet"; + char const * comment = NULL; + + if(setlocale(LC_ALL, "") == NULL) + _error("setlocale", 1); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + while((o = getopt(argc, argv, "uC:i:t:")) != -1) + switch(o) + { + case 'C': + comment = optarg; + break; + case 'i': + icon = optarg; + break; + case 't': + title = optarg; + break; + case 'u': + url = 1; + break; + default: + return _usage(); + } + if((optind + 1) != argc || url == 0) + return _usage(); + return (_bookmark(title, argv[optind], icon, comment) == 0) ? 0 : 2; +} diff --git a/tools/project.conf b/tools/project.conf index 264c799..86001c4 100644 --- a/tools/project.conf +++ b/tools/project.conf @@ -1,4 +1,4 @@ -targets=helper,htmlapp +targets=bookmark,helper,htmlapp cppflags_force=-I ../src #cppflags=-D EMBEDDED cflags_force=-W `pkg-config --cflags libDesktop` @@ -6,6 +6,13 @@ cflags=-Wall -g -O2 -pedantic ldflags=`pkg-config --libs libDesktop` dist=Makefile +[bookmark] +type=binary +sources=bookmark.c + +[bookmark.c] +depends=../config.h + [helper] type=binary #GtkMozEmbed