2009-08-27 03:35:32 -07:00
|
|
|
#include "Evry.h"
|
|
|
|
|
|
|
|
#define MAX_FUZZ 100
|
|
|
|
#define MAX_WORDS 5
|
|
|
|
|
2010-04-14 03:49:28 -07:00
|
|
|
static const char *home_dir = NULL;
|
|
|
|
static int home_dir_len;
|
|
|
|
static char dir_buf[1024];
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
evry_util_file_detail_set(Evry_Item_File *file)
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
if (!home_dir)
|
|
|
|
{
|
|
|
|
home_dir = e_user_homedir_get();
|
|
|
|
home_dir_len = strlen(home_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EVRY_ITEM(file)->browseable)
|
|
|
|
path = ecore_file_dir_get(file->path);
|
|
|
|
else
|
|
|
|
path = file->path;
|
|
|
|
|
|
|
|
if (path && !strncmp(path, home_dir, home_dir_len))
|
|
|
|
{
|
2010-04-16 07:15:30 -07:00
|
|
|
snprintf(dir_buf, sizeof(dir_buf), "~%s/", path + home_dir_len);
|
2010-04-14 03:49:28 -07:00
|
|
|
EVRY_ITEM(file)->detail = eina_stringshare_add(dir_buf);
|
|
|
|
}
|
|
|
|
else
|
2010-04-15 06:58:44 -07:00
|
|
|
{
|
|
|
|
if (!strncmp(path, "//", 2)) path++;
|
|
|
|
|
|
|
|
EVRY_ITEM(file)->detail = eina_stringshare_add(path);
|
|
|
|
}
|
2010-04-14 03:49:28 -07:00
|
|
|
}
|
2009-08-27 03:35:32 -07:00
|
|
|
|
|
|
|
EAPI int
|
|
|
|
evry_fuzzy_match(const char *str, const char *match)
|
|
|
|
{
|
|
|
|
const char *p, *m, *next;
|
|
|
|
int sum = 0;
|
|
|
|
|
|
|
|
unsigned int last = 0;
|
|
|
|
unsigned int offset = 0;
|
|
|
|
unsigned int min = 0;
|
|
|
|
unsigned char first = 0;
|
|
|
|
/* ignore punctuation */
|
|
|
|
unsigned char ip = 1;
|
|
|
|
|
|
|
|
unsigned int cnt = 0;
|
|
|
|
/* words in match */
|
|
|
|
unsigned int m_num = 0;
|
|
|
|
unsigned int m_cnt = 0;
|
|
|
|
unsigned int m_min[MAX_WORDS];
|
|
|
|
unsigned int m_len = 0;
|
|
|
|
|
|
|
|
if (!match || !str) return 0;
|
|
|
|
|
|
|
|
/* remove white spaces at the beginning */
|
|
|
|
for (; (*match != 0) && isspace(*match); match++);
|
|
|
|
for (; (*str != 0) && isspace(*str); str++);
|
|
|
|
|
|
|
|
/* count words in match */
|
|
|
|
for (m = match; (*m != 0) && (m_num < MAX_WORDS);)
|
|
|
|
{
|
|
|
|
for (; (*m != 0) && !isspace(*m); m++);
|
|
|
|
for (; (*m != 0) && isspace(*m); m++);
|
|
|
|
m_min[m_num++] = MAX_FUZZ;
|
|
|
|
}
|
|
|
|
for (m = match; ip && (*m != 0); m++)
|
|
|
|
if (ip && ispunct(*m)) ip = 0;
|
|
|
|
|
|
|
|
m_len = strlen(match);
|
|
|
|
|
|
|
|
/* with less than 3 chars match must be a prefix */
|
|
|
|
if (m_len < 3) m_len = 0;
|
|
|
|
|
|
|
|
next = str;
|
|
|
|
m = match;
|
|
|
|
|
|
|
|
while((m_cnt < m_num) && (*next != 0))
|
|
|
|
{
|
|
|
|
/* reset match */
|
|
|
|
if (m_cnt == 0) m = match;
|
|
|
|
|
|
|
|
/* end of matching */
|
|
|
|
if (*m == 0) break;
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
last = 0;
|
|
|
|
min = 1;
|
|
|
|
first = 0;
|
|
|
|
|
|
|
|
/* match current word of string against current match */
|
|
|
|
for (p = next; *next != 0; p++)
|
|
|
|
{
|
|
|
|
/* new word of string begins */
|
|
|
|
if ((*p == 0) || isspace(*p) || (ip && ispunct(*p)))
|
|
|
|
{
|
|
|
|
if (m_cnt < m_num - 1)
|
|
|
|
{
|
|
|
|
/* test next match */
|
|
|
|
for (; (*m != 0) && !isspace(*m); m++);
|
|
|
|
for (; (*m != 0) && isspace(*m); m++);
|
|
|
|
m_cnt++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* go to next word */
|
|
|
|
for (; (*p != 0) && ((isspace(*p) || (ip && ispunct(*p)))); p++);
|
|
|
|
cnt++;
|
|
|
|
next = p;
|
|
|
|
m_cnt = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* current char matches? */
|
|
|
|
if (tolower(*p) != tolower(*m))
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
offset += 1;
|
|
|
|
else
|
|
|
|
offset += 3;
|
|
|
|
|
|
|
|
if (offset <= m_len * 3)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min < MAX_FUZZ && offset <= m_len * 3)
|
|
|
|
{
|
|
|
|
/* first offset of match in word */
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
first = 1;
|
|
|
|
last = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
min += offset + (offset - last) * 5;
|
|
|
|
last = offset;
|
|
|
|
|
|
|
|
/* try next char of match */
|
|
|
|
if (*(++m) != 0 && !isspace(*m))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* end of match: store min weight of match */
|
|
|
|
min += (cnt - m_cnt) > 0 ? (cnt - m_cnt) : 0;
|
|
|
|
|
|
|
|
if (min < m_min[m_cnt])
|
|
|
|
m_min[m_cnt] = min;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* go to next match */
|
|
|
|
for (; (*m != 0) && !isspace(*m); m++);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_cnt < m_num - 1)
|
|
|
|
{
|
|
|
|
/* test next match */
|
|
|
|
for (; (*m != 0) && isspace(*m); m++);
|
|
|
|
m_cnt++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(*p != 0)
|
|
|
|
{
|
|
|
|
/* go to next word */
|
|
|
|
for (; (*p != 0) && !((isspace(*p) || (ip && ispunct(*p)))); p++);
|
|
|
|
for (; (*p != 0) && ((isspace(*p) || (ip && ispunct(*p)))); p++);
|
|
|
|
cnt++;
|
|
|
|
next = p;
|
|
|
|
m_cnt = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (m_cnt = 0; m_cnt < m_num; m_cnt++)
|
|
|
|
{
|
|
|
|
sum += m_min[m_cnt];
|
|
|
|
|
|
|
|
if (sum >= MAX_FUZZ)
|
|
|
|
{
|
|
|
|
sum = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-15 08:46:12 -07:00
|
|
|
if (sum > 0)
|
|
|
|
{
|
|
|
|
/* exact match ? */
|
|
|
|
if (strlen(str) != m_len)
|
|
|
|
sum += 10;
|
|
|
|
}
|
|
|
|
|
2009-08-27 03:35:32 -07:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_evry_fuzzy_match_sort_cb(const void *data1, const void *data2)
|
|
|
|
{
|
|
|
|
const Evry_Item *it1 = data1;
|
|
|
|
const Evry_Item *it2 = data2;
|
|
|
|
|
|
|
|
if (it1->priority - it2->priority)
|
|
|
|
return (it1->priority - it2->priority);
|
|
|
|
|
|
|
|
if (it1->fuzzy_match || it2->fuzzy_match)
|
|
|
|
{
|
|
|
|
if (it1->fuzzy_match && !it2->fuzzy_match)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!it1->fuzzy_match && it2->fuzzy_match)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (it1->fuzzy_match - it2->fuzzy_match)
|
|
|
|
return (it1->fuzzy_match - it2->fuzzy_match);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_List *
|
|
|
|
evry_fuzzy_match_sort(Eina_List *items)
|
|
|
|
{
|
|
|
|
return eina_list_sort(items, eina_list_count(items), _evry_fuzzy_match_sort_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* taken from e_utils. just changed 48 to 72.. we need
|
|
|
|
evry_icon_theme_set(Evas_Object *obj, const char *icon,
|
|
|
|
size:small, mid, large) imo */
|
|
|
|
static int
|
|
|
|
_evry_icon_theme_set(Evas_Object *obj, const char *icon)
|
|
|
|
{
|
|
|
|
const char *file;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
if ((!icon) || (!icon[0])) return 0;
|
|
|
|
snprintf(buf, sizeof(buf), "e/icons/%s", icon);
|
|
|
|
file = e_theme_edje_file_get("base/theme/icons", buf);
|
|
|
|
if (file[0])
|
|
|
|
{
|
|
|
|
e_icon_file_edje_set(obj, file, buf);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_evry_icon_fdo_set(Evas_Object *obj, const char *icon)
|
|
|
|
{
|
|
|
|
char *path = NULL;
|
|
|
|
unsigned int size;
|
|
|
|
|
|
|
|
if ((!icon) || (!icon[0])) return 0;
|
|
|
|
size = e_util_icon_size_normalize(72 * e_scale);
|
|
|
|
path = efreet_icon_path_find(e_config->icon_theme, icon, size);
|
|
|
|
|
|
|
|
if (!path) return 0;
|
|
|
|
e_icon_file_set(obj, path);
|
|
|
|
E_FREE(path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Evas_Object *
|
|
|
|
evry_icon_theme_get(const char *icon, Evas *e)
|
|
|
|
{
|
|
|
|
Evas_Object *o = e_icon_add(e);
|
|
|
|
|
|
|
|
if (e_config->icon_theme_overrides)
|
|
|
|
{
|
|
|
|
if (_evry_icon_fdo_set(o, icon) ||
|
|
|
|
_evry_icon_theme_set(o, icon))
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_evry_icon_theme_set(o, icon) ||
|
|
|
|
_evry_icon_fdo_set(o, icon))
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
evas_object_del(o);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Evas_Object *
|
|
|
|
evry_icon_mime_get(const char *mime, Evas *e)
|
|
|
|
{
|
|
|
|
Evas_Object *o = NULL;
|
|
|
|
char *icon;
|
|
|
|
|
|
|
|
icon = efreet_mime_type_icon_get(mime, e_config->icon_theme, 64);
|
|
|
|
|
|
|
|
if (icon)
|
|
|
|
{
|
|
|
|
o = e_util_icon_add(icon, e);
|
|
|
|
free(icon);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
o = evry_icon_theme_get("none", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2009-08-27 19:25:13 -07:00
|
|
|
EAPI int
|
|
|
|
evry_util_exec_app(const Evry_Item *it_app, const Evry_Item *it_file)
|
|
|
|
{
|
|
|
|
E_Zone *zone;
|
|
|
|
Eina_List *files = NULL;
|
|
|
|
char *exe = NULL;
|
|
|
|
|
|
|
|
if (!it_app) return 0;
|
|
|
|
ITEM_APP(app, it_app);
|
|
|
|
|
|
|
|
zone = e_util_zone_current_get(e_manager_current_get());
|
|
|
|
|
|
|
|
if (app->desktop)
|
|
|
|
{
|
|
|
|
if (it_file)
|
|
|
|
{
|
|
|
|
ITEM_FILE(file, it_file);
|
|
|
|
|
|
|
|
Eina_List *l;
|
|
|
|
char *mime;
|
|
|
|
char *path = NULL;
|
|
|
|
int open_folder = 0;
|
|
|
|
|
|
|
|
if (!EVRY_ITEM(file)->browseable)
|
|
|
|
{
|
|
|
|
EINA_LIST_FOREACH(app->desktop->mime_types, l, mime)
|
|
|
|
{
|
|
|
|
if (!strcmp(mime, "x-directory/normal"))
|
|
|
|
{
|
|
|
|
open_folder = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (open_folder)
|
|
|
|
{
|
2010-04-07 08:03:27 -07:00
|
|
|
path = ecore_file_dir_get(file->path);
|
2009-08-27 19:25:13 -07:00
|
|
|
files = eina_list_append(files, path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-07 08:03:27 -07:00
|
|
|
files = eina_list_append(files, file->path);
|
2009-08-27 19:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
e_exec(zone, app->desktop, NULL, files, NULL);
|
|
|
|
|
|
|
|
if (file && file->mime && !open_folder)
|
|
|
|
e_exehist_mime_desktop_add(file->mime, app->desktop);
|
|
|
|
|
|
|
|
if (files)
|
|
|
|
eina_list_free(files);
|
|
|
|
|
|
|
|
if (open_folder && path)
|
|
|
|
free(path);
|
|
|
|
}
|
2010-04-12 09:43:57 -07:00
|
|
|
else if (app->file)
|
|
|
|
{
|
2010-04-14 13:21:56 -07:00
|
|
|
files = eina_list_append(files, app->file);
|
|
|
|
e_exec(zone, app->desktop, NULL, files, NULL);
|
|
|
|
eina_list_free(files);
|
2010-04-12 09:43:57 -07:00
|
|
|
}
|
2009-08-27 19:25:13 -07:00
|
|
|
else
|
2010-04-12 09:43:57 -07:00
|
|
|
{
|
|
|
|
e_exec(zone, app->desktop, NULL, NULL, "everything");
|
|
|
|
}
|
2009-08-27 19:25:13 -07:00
|
|
|
}
|
|
|
|
else if (app->file)
|
|
|
|
{
|
|
|
|
if (it_file)
|
|
|
|
{
|
|
|
|
ITEM_FILE(file, it_file);
|
|
|
|
|
2010-04-07 08:03:27 -07:00
|
|
|
/* files = eina_list_append(files, file->path);
|
2010-04-01 21:19:38 -07:00
|
|
|
*
|
|
|
|
* e_exec(zone, NULL, app->file, files, NULL);
|
|
|
|
*
|
|
|
|
* if (files)
|
|
|
|
* eina_list_free(files); */
|
|
|
|
|
|
|
|
char *tmp;
|
|
|
|
int len;
|
2010-04-07 08:03:27 -07:00
|
|
|
tmp = eina_str_escape(file->path);
|
2010-04-01 21:19:38 -07:00
|
|
|
len = strlen(app->file) + strlen(tmp) + 2;
|
2009-08-27 19:25:13 -07:00
|
|
|
exe = malloc(len);
|
2010-04-01 21:19:38 -07:00
|
|
|
snprintf(exe, len, "%s %s", app->file, tmp);
|
2009-08-27 19:25:13 -07:00
|
|
|
e_exec(zone, NULL, exe, NULL, NULL);
|
2010-04-01 21:19:38 -07:00
|
|
|
E_FREE(exe);
|
|
|
|
E_FREE(tmp);
|
2009-08-27 19:25:13 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exe = (char *) app->file;
|
|
|
|
e_exec(zone, NULL, exe, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2010-04-07 08:03:27 -07:00
|
|
|
|
|
|
|
/* taken from curl:
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et
|
|
|
|
* al.
|
|
|
|
*
|
|
|
|
* Unescapes the given URL escaped string of given length. Returns a
|
|
|
|
* pointer to a malloced string with length given in *olen.
|
|
|
|
* If length == 0, the length is assumed to be strlen(string).
|
|
|
|
* If olen == NULL, no output length is stored.
|
|
|
|
*/
|
|
|
|
#define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x)))
|
|
|
|
|
|
|
|
EAPI char *
|
|
|
|
evry_util_unescape(const char *string, int length)
|
|
|
|
{
|
|
|
|
int alloc = (length?length:(int)strlen(string))+1;
|
|
|
|
char *ns = malloc(alloc);
|
|
|
|
unsigned char in;
|
|
|
|
int strindex=0;
|
|
|
|
unsigned long hex;
|
|
|
|
|
|
|
|
if( !ns )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while(--alloc > 0) {
|
|
|
|
in = *string;
|
|
|
|
if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
|
|
|
|
/* this is two hexadecimal digits following a '%' */
|
|
|
|
char hexstr[3];
|
|
|
|
char *ptr;
|
|
|
|
hexstr[0] = string[1];
|
|
|
|
hexstr[1] = string[2];
|
|
|
|
hexstr[2] = 0;
|
|
|
|
|
|
|
|
hex = strtoul(hexstr, &ptr, 16);
|
|
|
|
in = (unsigned char)(hex & (unsigned long) 0xFF);
|
|
|
|
// in = ultouc(hex); /* this long is never bigger than 255 anyway */
|
|
|
|
|
|
|
|
string+=2;
|
|
|
|
alloc-=2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ns[strindex++] = in;
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
ns[strindex]=0; /* terminate it */
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ISXDIGIT
|