Import a new tool to create bookmarks
This commit is contained in:
parent
dc3759a03c
commit
85ccab1720
152
tools/bookmark.c
Normal file
152
tools/bookmark.c
Normal file
|
@ -0,0 +1,152 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2015 Pierre Pronchery <khorben@defora.org> */
|
||||
/* 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 <http://www.gnu.org/licenses/>. */
|
||||
|
||||
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <libintl.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <System.h>
|
||||
#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;
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user