Efiler: Add mimetype handling via mailcap file. Context menu can now recognize several operation depending on file type

This commit is contained in:
Vedran Ljubovic 2008-06-08 20:34:44 +00:00
parent 936ecaadf7
commit 952b1338c9
3 changed files with 327 additions and 23 deletions

View File

@ -25,23 +25,23 @@
#include <Fl/Fl.H>
#include <Fl/Fl_Double_Window.H>
#include <Fl/Fl_Menu_Bar.H>
#include <Fl/Fl_File_Chooser.H> // for fl_dir_chooser, used in "Open location"
#include <Fl/filename.H>
#include <Fl/Fl_File_Input.H> // location bar
#include <FL/Fl_Shared_Image.H> // for fl_register_images()
#include <edelib/Nls.h>
#include <edelib/MimeType.h>
#include <edelib/String.h>
#include <edelib/StrUtil.h>
#include <edelib/Run.h>
#include <edelib/Directory.h>
#include <edelib/DirWatch.h>
#include <edelib/IconTheme.h> // for setting the icon theme
#include <edelib/MessageBox.h>
#include <edelib/DirWatch.h>
#include <edelib/Window.h>
#include <edelib/MimeType.h>
#include <edelib/Nls.h>
#include <edelib/Resource.h>
#include <edelib/Run.h>
#include <edelib/String.h>
#include <edelib/StrUtil.h>
#include <edelib/Window.h>
#include "EDE_FileView.h" // our file view widget
#include "EDE_DirTree.h" // directory tree
@ -50,6 +50,7 @@
#include "fileops.h" // file operations
#include "filesystem.h" // filesystem support
#include "ede_strverscmp.h" // local copy of strverscmp
#include "mailcap.h" // handling mailcap file
@ -121,7 +122,7 @@ public:
Some improvements to Fl_File_Input, that should be added
upstream:
* enable to set shortcut in label (STR 1770 for Fl_Input_)
* navigate using Ctrl+arrow (here we jump to dir. sep.)
* navigate using Ctrl+arrow (jump to dir separator)
-------------------------------------------------------------------*/
class My_File_Input : public Fl_File_Input {
@ -340,12 +341,12 @@ void loaddir(const char *path) {
// fl_filename_isdir() thinks that / isn't a dir :(
if (strcmp(path,"/")==0 || fl_filename_isdir(path)) {
if (path[0] == '~' && path[1] == '/') {// Expand tilde
snprintf(current_dir,FL_PATH_MAX,"%s/%s",getenv("HOME"),tmpath+1);
snprintf(current_dir,FL_PATH_MAX,"%s/%s",edelib::dir_home().c_str(),tmpath+1);
} else if (path[0] != '/') { // Relative path
char *t = tmpath;
if (path[0] == '.' && path[1] == '/') t+=2; // remove .
else if (path[0] == '.' && path[1] != '.') t+=1;
snprintf(current_dir,FL_PATH_MAX,"%s/%s",getenv("PWD"),t);
snprintf(current_dir,FL_PATH_MAX,"%s/%s",edelib::dir_current().c_str(),t);
} else {
if (path!=current_dir) strncpy(current_dir,tmpath,strlen(tmpath)+1);
}
@ -539,7 +540,10 @@ void open_cb(Fl_Widget*w, void*data) {
// Find opener
mime_resolver.set(path);
char* opener = simpleopener(mime_resolver.type().c_str());
// char* opener = simpleopener(mime_resolver.type().c_str());
int tmp=(int)data; // cast data for passing to mailcap_opener
if (tmp<1 || tmp>8) tmp=1;
const char* opener = mailcap_opener(mime_resolver.type().c_str(), (MailcapAction)tmp);
// dump core
struct rlimit *rlim = (struct rlimit*)malloc(sizeof(struct rlimit));
@ -551,10 +555,13 @@ void open_cb(Fl_Widget*w, void*data) {
const char *o2 = tsprintf(opener,path); // opener should contain %s
fprintf (stderr, "run_program: %s\n", o2);
if (opener) {
if (tmp==16) { // 16 - open script
int k=edelib::run_program(path,false); fprintf(stderr, "retval: %d\n", k);
}
else if (opener) {
int k=edelib::run_program(o2,false); fprintf(stderr, "retval: %d\n", k);
} else
statusbar->copy_label(tsprintf(_("No program to open %s!"), fl_filename_name(path)));
statusbar->copy_label(tsprintf(_("No program to open %s! (%s)"), fl_filename_name(path),mime_resolver.type().c_str()));
rlim->rlim_cur = old_rlimit;
setrlimit (RLIMIT_CORE, rlim);
@ -643,7 +650,7 @@ void update_menu_item(const char* label, bool value);
void ow_cb(Fl_Widget*, void*) {
const char* app = edelib::input(_("Enter the name of application to open this file with:"));
const char* file = view->path(view->get_focus());
edelib::run_program(tsprintf("%s %s", app, file), /*wait=*/false);
edelib::run_program(tsprintf("%s '%s'", app, file), /*wait=*/false);
}
@ -822,7 +829,11 @@ void directory_change_cb(const char* dir, const char* what_changed, int flags, v
// every item should have own context menu defined
Fl_Menu_Item context_menu_definition[] = {
{_("&Open"), 0, open_cb},
{_("&Open"), 0, open_cb, (void*)1,},
{_("&View"), 0, open_cb, (void*)1,FL_MENU_INVISIBLE},
{_("&Edit"), 0, open_cb, (void*)4,FL_MENU_INVISIBLE},
{_("Pri&nt"), 0, open_cb, (void*)8,FL_MENU_INVISIBLE},
{_("&Execute"), 0, open_cb, (void*)16,FL_MENU_INVISIBLE},
{_("Open &with..."), 0, ow_cb, 0,FL_MENU_DIVIDER},
{_("&Cut"), 0, cut_cb},
{_("Co&py"), 0, copy_cb},
@ -832,8 +843,42 @@ Fl_Menu_Item context_menu_definition[] = {
{0}
};
bool file_executable(const char* path) {
if (stat64(path,&stat_buffer)) return false; // error
static uid_t uid = stat_buffer.st_uid, uuid = getuid();
static gid_t gid = stat_buffer.st_gid, ugid = getgid();
mode_t mode = stat_buffer.st_mode;
if (S_ISDIR(mode)) return false;
if (uid == uuid && mode&S_IXUSR) return true;
if (gid == ugid && mode&S_IXGRP) return true;
if (mode&S_IXOTH) return true;
return false;
}
// Right click - show context menu
void context_cb(Fl_Widget*, void*) {
char* path = (char*)view->path(view->get_focus());
mime_resolver.set(path);
int actions = mailcap_actions(mime_resolver.type().c_str());
Fl_Menu_Item *k = context_menu_definition;
while(k->label()!=0) {
if (k->label()!=0 && strcmp(k->label(),_("&Open"))==0)
if (actions&MAILCAP_EDIT) k->hide(); else k->show();
if (k->label()!=0 && (strcmp(k->label(),_("&View"))==0 || strcmp(k->label(),_("&Edit"))==0))
if (actions&MAILCAP_EDIT) k->show(); else k->hide();
if (k->label()!=0 && strcmp(k->label(),_("Pri&nt"))==0)
if (actions&MAILCAP_PRINT) k->show(); else k->hide();
if (k->label()!=0 && strcmp(k->label(),_("&Execute"))==0)
if (file_executable(path)) k->show(); else k->hide();
k++;
}
context_menu->popup();
context_menu->value(-1);
}
@ -847,7 +892,7 @@ void load_preferences() {
bool icons_view=false;
int winw, winh;
app_config.load("efiler");
app_config.load("ede/efiler");
app_config.get("gui","show_hidden",showhidden,false); // Show hidden files
app_config.get("gui","dirs_first",dirsfirst,true); // Directories before ordinary files
app_config.get("gui","ignore_case",ignorecase,true); // Ignore case when sorting
@ -891,7 +936,7 @@ void save_preferences() {
app_config.set("gui","window_width",win->w()); // Window dimensions
app_config.set("gui","window_height",win->h());
app_config.save("efiler");
app_config.save("ede/efiler");
}
@ -952,27 +997,34 @@ void update_menu_item(const char* label, bool value) {
}
int parsecmd(int argc, char**argv, int &index) {
if ((strcmp(argv[index],"-ic")==0) || (strcmp(argv[index],"-icons")==0)) {
edelib::IconTheme::init(argv[++index]);
++index;
return 1;
}
return 0;
}
// Main program
extern int FL_NORMAL_SIZE;
int main (int argc, char **argv) {
// Parse command line - this must come first
int unknown=0;
Fl::args(argc,argv,unknown);
Fl::args(argc,argv,unknown,parsecmd);
if (unknown==argc)
snprintf(current_dir, FL_PATH_MAX, getenv("HOME"));
snprintf(current_dir, FL_PATH_MAX, edelib::dir_home().c_str());
else {
if (strcmp(argv[unknown],"--help")==0) {
printf(_("EFiler - EDE File Manager\nPart of Equinox Desktop Environment (EDE).\nCopyright (c) 2000-2007 EDE Authors.\n\nThis program is licenced under terms of the\nGNU General Public Licence version 2 or newer.\nSee COPYING for details.\n\n"));
printf(_("Usage:\n\tefiler [OPTIONS] [PATH]\n\n"));
printf(_("Options:\n%s\n"),Fl::help);
printf(_("Options:\n -ic[ons] icon theme\n%s\n"),Fl::help);
return 1;
}
strncpy(current_dir, argv[unknown], strlen(argv[unknown])+1);
}
edelib::IconTheme::init("crystalsvg");
edelib::DirWatch::init();
edelib::DirWatch::callback(directory_change_cb);
// Let other components know if we have notify

213
efiler/mailcap.cpp Normal file
View File

@ -0,0 +1,213 @@
/*
* $Id$
*
* EFiler - EDE File Manager
* Part of Equinox Desktop Environment (EDE).
* Copyright (c) 2000-2007 EDE Authors.
*
* This program is licenced under terms of the
* GNU General Public Licence version 2 or newer.
* See COPYING for details.
*/
// References:
// [1] Mutt documentation, chapter on mailcap
// http://www.mutt.org/doc/manual/manual-5.html
// [2] RFC 1524
// http://www.faqs.org/rfcs/rfc1524.html
#include "mailcap.h"
#include <edelib/String.h>
#include <edelib/List.h>
#include <edelib/Util.h>
#include <edelib/Directory.h>
#include <edelib/File.h>
#include <edelib/StrUtil.h>
#include <stdio.h>
// See: [2] Appendix A
#define MAILCAPS "/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap"
// FIXME: use favourite aplications to find xterm
#define TERM "xterm"
using namespace edelib; // this drastically shortened the code...
struct mime_db_struct {
String type;
String viewcmd;
String createcmd;
String editcmd;
String printcmd;
};
list<mime_db_struct> mime_db;
bool files_read=false;
// Expand various macros in mailcap command spec and prevent
// certain security issues
String mailcap_sanitize(char* cmd, String type) {
String s(cmd);
unsigned int k = s.find("%s");
// no %s, file should be piped to stdin
if (k==String::npos) {
s = String("cat '%s' | ") + cmd;
} else {
// replace %s with '%s' (see: [1])
while (k!=String::npos) {
s = s.substr(0,k)+ "'%s'" + s.substr(k+2);
k = s.find("%s",k+2);
}
}
// replace %t with mimetype
k = s.find("%t");
while (k!=String::npos) {
s = s.substr(0,k) + type + s.substr(k+2);
k = s.find("%t",k+1);
}
// we don't know the charset
k = s.find("%{charset}");
while (k!=String::npos) {
s = s.substr(0,k) + "us-ascii" + s.substr(k+10);
k = s.find("%{charset}",k+1);
}
// ignore other mutt specific codes
k = s.find("%{");
while (k!=String::npos) {
s = s.substr(0,k) + s.substr(k+2);
k = s.find("}",k); // there has to be a closing brace...
if (k!=String::npos) s= s.substr(0,k) + s.substr(k+1);
k = s.find("%{",k);
}
return s;
}
void read_files() {
list<String> locations;
// RFC compliant list of mailcap locations
stringtok(locations, String(MAILCAPS), ":");
// Add ~/.mailcap
locations.push_front(dir_home() + E_DIR_SEPARATOR_STR".mailcap");
// Add EDE specific configuracion
if (user_config_dir() != "")
locations.push_front(user_config_dir() + E_DIR_SEPARATOR_STR"ede"E_DIR_SEPARATOR_STR"mailcap");
// Test all these locations
const char* mailcap = NULL;
{
list<String>::iterator it = locations.begin(), it_end = locations.end();
for(; it != it_end; ++it) {
if (file_exists((*it).c_str())) {
mailcap=(*it).c_str();
break;
}
}
}
fprintf (stderr, " ------ mailcap: %s\n",mailcap);
// Read data into list
FILE *fp = fopen(mailcap,"r");
char buffer[1000];
while (fgets(buffer,1000,fp)) {
str_trim(buffer);
if (buffer[0]=='#') continue; //comment
if (strlen(buffer)<2) continue; //empty line
// Multiline
if (buffer[strlen(buffer)-1]=='\\') {
String line(buffer); //Use string for dynamic growing
while (fgets(buffer,1000,fp)) {
str_trim(buffer);
line = line.substr(0, line.length()-2);
line += buffer;
if (buffer[strlen(buffer)-1]!='\\') break;
}
// Use just the first 1000 chars
strncpy(buffer, line.c_str(), 1000);
buffer[999]='\0';
}
// Parse line
mime_db_struct parsed;
char *tmp = strtok(buffer, ";");
str_trim(tmp);
parsed.type=tmp;
// View command
tmp = strtok(NULL, ";");
if (tmp==NULL) continue; // no view cmd in line!?
str_trim(tmp);
parsed.viewcmd = mailcap_sanitize(tmp, parsed.type);
// Other mailcap parameters...
while ((tmp = strtok(NULL, ";")) != NULL) {
str_trim(tmp);
if (strcmp(tmp,"copiousoutput")==0)
// TODO: use enotepad? when it supports stdin
parsed.viewcmd = TERM" -e \"" + parsed.viewcmd + " | less\"";
else if (strcmp(tmp,"needsterminal")==0)
parsed.viewcmd = TERM" -e \"" + parsed.viewcmd + "\"";
else if (strncmp(tmp,"compose=",8)==0)
parsed.createcmd = mailcap_sanitize(tmp+8, parsed.type);
else if (strncmp(tmp,"print=",6)==0)
parsed.printcmd = mailcap_sanitize(tmp+6, parsed.type);
else if (strncmp(tmp,"edit=",5)==0)
parsed.editcmd = mailcap_sanitize(tmp+5, parsed.type);
// We ignore other parameters
}
mime_db.push_back(parsed);
}
fclose(fp);
files_read=true;
}
list<mime_db_struct>::iterator find_type(const char* type) {
list<mime_db_struct>::iterator it = mime_db.begin(), it_end = mime_db.end();
for(; it != it_end; ++it)
if ((*it).type==type) return it;
it = mime_db.begin();
for(; it != it_end; ++it) {
String s((*it).type);
if (s[s.length()-1]=='*' && strncmp(type, s.c_str(), s.length()-1)==0)
return it;
}
return 0;
}
const char* mailcap_opener(const char* type, MailcapAction action) {
if (!files_read) read_files();
list<mime_db_struct>::iterator it = find_type(type);
if (it==0) return 0;
if (action==MAILCAP_VIEW) return (*it).viewcmd.c_str();
else if (action==MAILCAP_CREATE) return (*it).createcmd.c_str();
else if (action==MAILCAP_EDIT) return (*it).editcmd.c_str();
else if (action==MAILCAP_PRINT) return (*it).printcmd.c_str();
else return 0;
}
int mailcap_actions(const char* type) {
if (!files_read) read_files();
list<mime_db_struct>::iterator it = find_type(type);
if (it==0) return 0;
int result=0;
if ((*it).viewcmd != "") result += MAILCAP_VIEW;
if ((*it).createcmd != "") result += MAILCAP_CREATE;
if ((*it).editcmd != "") result += MAILCAP_EDIT;
if ((*it).printcmd != "") result += MAILCAP_PRINT;
return result;
}

39
efiler/mailcap.h Normal file
View File

@ -0,0 +1,39 @@
/*
* $Id$
*
* EFiler - EDE File Manager
* Part of Equinox Desktop Environment (EDE).
* Copyright (c) 2000-2007 EDE Authors.
*
* This program is licenced under terms of the
* GNU General Public Licence version 2 or newer.
* See COPYING for details.
*/
#ifndef mailcap_h
#define mailcap_h
enum MailcapAction {
MAILCAP_VIEW = 1,
MAILCAP_CREATE = 2,
MAILCAP_EDIT = 4,
MAILCAP_PRINT = 8
};
// Returns a shell command to be used for opening files of given type.
// The command returned will *always* contain %s which should be replaced
// with filename. Parameter MAILCAP_TYPE indicates the type of action to
// be performed
const char* mailcap_opener(const char* type, MailcapAction action=MAILCAP_VIEW);
// Returns a list of actions available for a given type
int mailcap_actions(const char* type);
#endif