Resurrect the -f option

It has been removed when the extentions feature has been introduced.

Now, when -f is used in the command line, a list of extensions is
displayed to request the user to choose the extension that needs to
handle this file.
This commit is contained in:
Daniel Zaoui 2017-05-20 23:52:38 +03:00
parent ee6fbd81ea
commit ec13d9020f
3 changed files with 182 additions and 46 deletions

View File

@ -88,6 +88,7 @@ static Connection_Type _conn_type = OFFLINE;
static Eina_Debug_Session *_session = NULL;
static Eina_List *_apps = NULL;
static App_Info *_selected_app = NULL;
static Eina_Stringshare *_offline_filename = NULL;
static Eet_Data_Descriptor *_profile_edd = NULL, *_config_edd = NULL;
static Eina_List *_profiles = NULL;
@ -671,20 +672,20 @@ _extension_delete(Clouseau_Extension *ext)
free(ext);
}
static void
static Clouseau_Extension *
_extension_instantiate(Extension_Config *cfg)
{
Eina_List *itr, *itr2;
Clouseau_Extension *ext;
char path[1024];
if (!cfg->ready) return;
if (!cfg->ready) return NULL;
EINA_LIST_FOREACH_SAFE(_extensions, itr, itr2, ext) _extension_delete(ext);
ext = calloc(1, sizeof(*ext));
sprintf(path, "%s/clouseau/extensions", efreet_config_home_get());
if (!_mkdir(path)) return;
if (!_mkdir(path)) return NULL;
ext->path_to_config = eina_stringshare_add(path);
ext->ext_cfg = cfg;
@ -694,7 +695,7 @@ _extension_instantiate(Extension_Config *cfg)
{
printf("Error in extension_init function of %s\n", cfg->name);
free(ext);
return;
return NULL;
}
_extensions = eina_list_append(_extensions, ext);
@ -702,6 +703,8 @@ _extension_instantiate(Extension_Config *cfg)
_session_populate();
_app_populate();
return ext;
}
static void
@ -712,11 +715,104 @@ _extension_view(void *data,
_extension_instantiate(cfg);
}
static int
_file_get(const char *filename, char **buffer_out)
{
char *file_data = NULL;
int file_size;
FILE *fp = fopen(filename, "r");
if (!fp)
{
printf("Can not open file: \"%s\".\n", filename);
return -1;
}
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
if (file_size <= 0)
{
fclose(fp);
if (file_size < 0) printf("Can not ftell file: \"%s\".\n", filename);
return -1;
}
rewind(fp);
file_data = (char *) calloc(1, file_size);
if (!file_data)
{
fclose(fp);
printf("Calloc failed\n");
return -1;
}
int res = fread(file_data, 1, file_size, fp);
if (!res)
{
free(file_data);
file_data = NULL;
if (!feof(fp)) printf("fread failed\n");
}
fclose(fp);
if (file_data && buffer_out) *buffer_out = file_data;
return file_size;
}
static void
_extension_offline_load(void *data, Evas_Object *obj,
void *event_info EINA_UNUSED)
{
Extension_Config *cfg = data;
Clouseau_Extension *ext = _extension_instantiate(cfg);
char *buffer = NULL;
int size = _file_get(_offline_filename, &buffer);
if (size <= 0) return;
while (obj && strcmp(efl_class_name_get(obj), "Elm.Inwin"))
obj = efl_parent_get(obj);
if (obj) efl_del(obj);
_ui_freeze(ext, EINA_TRUE);
if (ext->import_data_cb) ext->import_data_cb(ext, buffer, size);
_ui_freeze(ext, EINA_FALSE);
}
static void
_extensions_cfgs_inwin_create()
{
Eina_List *itr;
Extension_Config *ext_cfg;
Eo *inwin = _inwin_create();
elm_object_style_set(inwin, "minimal");
Eo *box = elm_box_add(inwin);
evas_object_size_hint_weight_set(box, 1, 1);
evas_object_size_hint_align_set(box, -1, -1);
efl_gfx_visible_set(box, EINA_TRUE);
Eo *label = efl_add(ELM_LABEL_CLASS, box);
elm_object_text_set(label, "Choose an extension to open the file:");
evas_object_size_hint_align_set(label, 0, -1);
evas_object_size_hint_weight_set(label, 1, 1);
efl_gfx_visible_set(label, EINA_TRUE);
elm_box_pack_end(box, label);
Eo *list = elm_list_add(inwin);
elm_list_mode_set(list, ELM_LIST_EXPAND);
evas_object_size_hint_weight_set(list, 1, 1);
evas_object_size_hint_align_set(list, -1, -1);
EINA_LIST_FOREACH(_config->extensions_cfgs, itr, ext_cfg)
{
if (ext_cfg->ready)
elm_list_item_append(list, ext_cfg->name, NULL, NULL, _extension_offline_load, ext_cfg);
}
evas_object_show(list);
elm_box_pack_end(box, list);
elm_win_inwin_content_set(inwin, box);
elm_win_inwin_activate(inwin);
}
EAPI_MAIN int
elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
Connection_Type conn_type = OFFLINE;
const char *offline_filename = NULL;
Eina_List *itr;
Extension_Config *ext_cfg;
int i, long_index = 0, opt;
@ -732,11 +828,12 @@ elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{"help", no_argument, 0, 'h'},
{"local", no_argument, 0, 'l'},
{"remote", required_argument, 0, 'r'},
{"file", required_argument, 0, 'f'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv,"hlr:f:", long_options, &long_index )) != -1)
{
if (conn_type != OFFLINE || offline_filename)
if (conn_type != OFFLINE || _offline_filename)
{
printf("You cannot use more than one option at a time\n");
help = EINA_TRUE;
@ -759,6 +856,12 @@ elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
}
break;
}
case 'f':
{
conn_type = OFFLINE;
_offline_filename = eina_stringshare_add(optarg);
break;
}
case 'h': help = EINA_TRUE; break;
default: help = EINA_TRUE;
}
@ -769,6 +872,7 @@ elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
printf(" --help/-h Print that help\n");
printf(" --local/-l Create a local connection\n");
printf(" --remote/-r Create a remote connection by using the given profile name\n");
printf(" --file/-f Run in offline mode and load the given file\n");
return 0;
}
@ -799,11 +903,14 @@ elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
NULL, NULL, ext_cfg->name, _extension_view, ext_cfg);
if (!ext_cfg->ready) elm_object_item_disabled_set(it, EINA_TRUE);
}
if (eina_list_count(_config->extensions_cfgs) == 1)
_extension_instantiate(eina_list_data_get(_config->extensions_cfgs));
_connection_type_change(conn_type);
if (conn_type == OFFLINE && _offline_filename)
{
_extensions_cfgs_inwin_create();
}
elm_run();
_connection_type_change(OFFLINE);

View File

@ -35,6 +35,7 @@ typedef struct _Clouseau_Extension Clouseau_Extension;
typedef Eo *(*Ui_Get_Cb)(Clouseau_Extension *ext, Eo *parent);
typedef void (*Session_Changed_Cb)(Clouseau_Extension *ext);
typedef void (*App_Changed_Cb)(Clouseau_Extension *ext);
typedef void (*Import_Data_Cb)(Clouseau_Extension *ext, char *buffer, int size);
typedef Eo *(*Inwin_Create_Cb)();
typedef void (*Ui_Freeze_Cb)(Clouseau_Extension *ext, Eina_Bool freeze);
@ -48,6 +49,7 @@ struct _Clouseau_Extension
Eo *ui_object; /* Main object of the UI extension */
Session_Changed_Cb session_changed_cb; /* Function called when the session changed */
App_Changed_Cb app_changed_cb; /* Function called when the app changed */
Import_Data_Cb import_data_cb; /* Function called when data has to be imported */
Inwin_Create_Cb inwin_create_cb; /* Function to call to create a Inwin */
Ui_Freeze_Cb ui_freeze_cb; /* Function to call to freeze/thaw the UI */
void *data; /* Data allocated and managed by the extension */

View File

@ -383,59 +383,36 @@ end:
}
static Snapshot *
_snapshot_open(const char *in_file)
_snapshot_buffer_parse(char *buffer, int size)
{
FILE *fp = fopen(in_file, "r");
void *eet_buffer = NULL;
Snapshot *s = NULL;
int eet_size = 0;
if (!fp) return NULL;
if (fread(&eet_size, sizeof(int), 1, fp) != 1)
{
printf("Read size from %s failed\n", in_file);
goto end;
}
if (size < eet_size) return NULL;
memcpy(&eet_size, buffer, sizeof(int));
buffer += sizeof(int);
_snapshot_eet_load();
eet_buffer = malloc(eet_size);
if ((int)fread(eet_buffer, 1, eet_size, fp) != eet_size)
{
printf("Read EET buffer from %s failed\n", in_file);
goto end;
}
s = eet_data_descriptor_decode(_snapshot_edd, eet_buffer, eet_size);
s = eet_data_descriptor_decode(_snapshot_edd, buffer, eet_size);
buffer += eet_size;
if (s->cur_len)
{
s->buffer = malloc(s->cur_len);
if (fread(s->buffer, 1, s->cur_len, fp) != s->cur_len)
{
printf("Read snapshot buffer from %s failed\n", in_file);
free(s->buffer);
s->buffer = NULL;
goto end;
}
}
if (s->cur_len) s->buffer = buffer;
end:
if (fp) fclose(fp);
if (eet_buffer) free(eet_buffer);
return s;
}
static void
_snapshot_load(void *data, Evas_Object *fs EINA_UNUSED, void *ev)
_snapshot_load(Clouseau_Extension *ext, char *buffer, int size)
{
Evas_Debug_Screenshot *shot;
Clouseau_Extension *ext = data;
Snapshot *s = NULL;
unsigned int idx = 0;
if (!ext) return;
Instance *inst = ext->data;
s = _snapshot_open(ev);
Instance *inst = ext->data;
Snapshot *s = _snapshot_buffer_parse(buffer, size);
if (!s) return;
_session_changed(ext);
@ -449,7 +426,7 @@ _snapshot_load(void *data, Evas_Object *fs EINA_UNUSED, void *ev)
{
Eina_Debug_Packet_Header *hdr = (Eina_Debug_Packet_Header *)(s->buffer + idx);
void *payload = (s->buffer + idx) + sizeof(*hdr);
int size = hdr->size - sizeof(*hdr);
size = hdr->size - sizeof(*hdr);
if (hdr->opcode == _eoids_get_op) _eoids_get((Eina_Debug_Session *)ext, -1, payload, size);
else if (hdr->opcode == _klids_get_op) _klids_get((Eina_Debug_Session *)ext, -1, payload, size);
else if (hdr->opcode == _obj_info_op) _obj_info_get((Eina_Debug_Session *)ext, -1, payload, size);
@ -462,8 +439,57 @@ _snapshot_load(void *data, Evas_Object *fs EINA_UNUSED, void *ev)
info->screenshots = eina_list_append(info->screenshots, shot);
if (info->glitem) elm_genlist_item_update(info->glitem);
}
free(s->buffer);
free(s);
free(buffer);
}
static int
_file_get(const char *filename, char **buffer_out)
{
char *file_data = NULL;
int file_size;
FILE *fp = fopen(filename, "r");
if (!fp)
{
printf("Can not open file: \"%s\".\n", filename);
return -1;
}
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
if (file_size <= 0)
{
fclose(fp);
if (file_size < 0) printf("Can not ftell file: \"%s\".\n", filename);
return -1;
}
rewind(fp);
file_data = (char *) calloc(1, file_size);
if (!file_data)
{
fclose(fp);
printf("Calloc failed\n");
return -1;
}
int res = fread(file_data, 1, file_size, fp);
if (!res)
{
free(file_data);
file_data = NULL;
if (!feof(fp)) printf("fread failed\n");
}
fclose(fp);
if (file_data && buffer_out) *buffer_out = file_data;
return file_size;
}
static void
_snapshot_load_from_fs(void *data, Evas_Object *fs EINA_UNUSED, void *ev)
{
char *buffer = NULL;
int size = _file_get(ev, &buffer);
if (size <= 0) return;
_snapshot_load(data, buffer, size);
}
static void
@ -1217,7 +1243,7 @@ _fs_activate(Clouseau_Extension *ext, Eina_Bool is_save)
evas_object_size_hint_align_set(fs, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_smart_callback_add(fs, "done", _inwin_del, inwin);
evas_object_smart_callback_add(fs, "done",
is_save?_snapshot_do:_snapshot_load, ext);
is_save?_snapshot_do:_snapshot_load_from_fs, ext);
evas_object_show(fs);
elm_win_inwin_content_set(inwin, fs);
@ -1306,6 +1332,7 @@ extension_start(Clouseau_Extension *ext, Eo *parent)
ext->data = inst;
ext->session_changed_cb = _session_changed;
ext->app_changed_cb = _app_changed;
ext->import_data_cb = _snapshot_load;
inst->classes_hash_by_id = eina_hash_pointer_new(NULL);
inst->classes_hash_by_name = eina_hash_string_small_new(NULL);