mirror of
https://github.com/edeproject/ede.git
synced 2023-08-10 21:13:03 +03:00
- Add new entries to mailcap using Open with.. dialog
- Start of work on Properties window. For now, the code is very ugly and it is read only.
This commit is contained in:
@ -35,6 +35,13 @@
|
||||
// FIXME: use favourite aplications to find xterm
|
||||
#define TERM "xterm"
|
||||
|
||||
// Maximum length of a mailcap spec
|
||||
// We could've used edelib::String, but that would make the whole code a bit
|
||||
// more complex and a bit slower. There's no reason for mailcap to be longer
|
||||
// (very long mailcaps are usually application specific codes /e.g. mutt/ which
|
||||
// isn't useful to us)
|
||||
#define BUFLEN 1000
|
||||
|
||||
|
||||
using namespace edelib; // this drastically shortened the code...
|
||||
|
||||
@ -45,9 +52,15 @@ struct mime_db_struct {
|
||||
String createcmd;
|
||||
String editcmd;
|
||||
String printcmd;
|
||||
bool copiousoutput;
|
||||
bool needsterminal;
|
||||
};
|
||||
|
||||
|
||||
// database of parsed mimetypes
|
||||
list<mime_db_struct> mime_db;
|
||||
|
||||
// flag to know if we should call read_files()
|
||||
bool files_read=false;
|
||||
|
||||
|
||||
@ -56,9 +69,9 @@ bool files_read=false;
|
||||
|
||||
String mailcap_sanitize(char* cmd, String type) {
|
||||
String s(cmd);
|
||||
unsigned int k = s.find("%s");
|
||||
|
||||
// no %s, file should be piped to stdin
|
||||
unsigned int k = s.find("%s");
|
||||
if (k==String::npos) {
|
||||
s = String("cat '%s' | ") + cmd;
|
||||
} else {
|
||||
@ -87,12 +100,14 @@ String mailcap_sanitize(char* cmd, String type) {
|
||||
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);
|
||||
k = s.find("%{",k+1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
// Read contents of mailcap file
|
||||
|
||||
void read_files() {
|
||||
list<String> locations;
|
||||
|
||||
@ -100,7 +115,7 @@ void read_files() {
|
||||
stringtok(locations, String(MAILCAPS), ":");
|
||||
// Add ~/.mailcap
|
||||
locations.push_front(dir_home() + E_DIR_SEPARATOR_STR".mailcap");
|
||||
// Add EDE specific configuracion
|
||||
// Add EDE specific configuration
|
||||
if (user_config_dir() != "")
|
||||
locations.push_front(user_config_dir() + E_DIR_SEPARATOR_STR"ede"E_DIR_SEPARATOR_STR"mailcap");
|
||||
|
||||
@ -120,8 +135,8 @@ void read_files() {
|
||||
|
||||
// Read data into list
|
||||
FILE *fp = fopen(mailcap,"r");
|
||||
char buffer[1000];
|
||||
while (fgets(buffer,1000,fp)) {
|
||||
char buffer[BUFLEN];
|
||||
while (fgets(buffer,BUFLEN,fp)) {
|
||||
str_trim(buffer);
|
||||
if (buffer[0]=='#') continue; //comment
|
||||
if (strlen(buffer)<2) continue; //empty line
|
||||
@ -129,15 +144,15 @@ void read_files() {
|
||||
// Multiline
|
||||
if (buffer[strlen(buffer)-1]=='\\') {
|
||||
String line(buffer); //Use string for dynamic growing
|
||||
while (fgets(buffer,1000,fp)) {
|
||||
while (fgets(buffer,BUFLEN,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';
|
||||
// Use just the first BUFLEN chars
|
||||
strncpy(buffer, line.c_str(), BUFLEN);
|
||||
buffer[BUFLEN]='\0';
|
||||
}
|
||||
|
||||
// Parse line
|
||||
@ -146,20 +161,20 @@ void read_files() {
|
||||
str_trim(tmp);
|
||||
parsed.type=tmp;
|
||||
|
||||
// View command
|
||||
// Command for viewing file
|
||||
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...
|
||||
parsed.copiousoutput = parsed.needsterminal = false;
|
||||
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\"";
|
||||
parsed.copiousoutput = true;
|
||||
else if (strcmp(tmp,"needsterminal")==0)
|
||||
parsed.viewcmd = TERM" -e \"" + parsed.viewcmd + "\"";
|
||||
parsed.needsterminal = true;
|
||||
else if (strncmp(tmp,"compose=",8)==0)
|
||||
parsed.createcmd = mailcap_sanitize(tmp+8, parsed.type);
|
||||
else if (strncmp(tmp,"print=",6)==0)
|
||||
@ -175,6 +190,9 @@ void read_files() {
|
||||
files_read=true;
|
||||
}
|
||||
|
||||
|
||||
// Helper function for searching mime_db
|
||||
|
||||
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)
|
||||
@ -189,17 +207,36 @@ list<mime_db_struct>::iterator find_type(const char* type) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// Command for performing given action on files of given type
|
||||
|
||||
const char* mailcap_opener(const char* type, MailcapAction action) {
|
||||
static char buffer[BUFLEN];
|
||||
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();
|
||||
|
||||
if (action==MAILCAP_VIEW) {
|
||||
const char *c = (*it).viewcmd.c_str();
|
||||
if ((*it).copiousoutput) {
|
||||
// copiousoutput and needsterminal are mutually exclusive [2]
|
||||
// TODO: use enotepad? - when it supports stdin
|
||||
snprintf(buffer, BUFLEN-1, TERM" -e \"%s\" | less", c);
|
||||
} else if ((*it).needsterminal) {
|
||||
snprintf(buffer, BUFLEN-1, TERM" -e \"%s\"", c);
|
||||
} else return c;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// List of actions available for given type
|
||||
|
||||
int mailcap_actions(const char* type) {
|
||||
if (!files_read) read_files();
|
||||
list<mime_db_struct>::iterator it = find_type(type);
|
||||
@ -211,3 +248,62 @@ int mailcap_actions(const char* type) {
|
||||
if ((*it).printcmd != "") result += MAILCAP_PRINT;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// Add a new opener for given type
|
||||
// NOTE: This will be written in EDE-specific location. If EDE-specific
|
||||
// mailcap file doesn't exist, it will be created and existing mimetypes
|
||||
// will be added. Thus, users of EDE will get a new type in addition to
|
||||
// old ones, however other applications will not.
|
||||
|
||||
void mailcap_add_type(const char* type, const char* opener) {
|
||||
// Find old type, edit if it exists
|
||||
list<mime_db_struct>::iterator it = find_type(type);
|
||||
if (it != 0) {
|
||||
(*it).viewcmd = opener;
|
||||
(*it).viewcmd += " %s";
|
||||
} else {
|
||||
mime_db_struct newtype;
|
||||
newtype.type = type;
|
||||
newtype.viewcmd = opener;
|
||||
newtype.viewcmd += " %s";
|
||||
newtype.createcmd = "";
|
||||
newtype.editcmd = "";
|
||||
newtype.printcmd = "";
|
||||
newtype.copiousoutput = false;
|
||||
newtype.needsterminal = false;
|
||||
mime_db.push_back(newtype);
|
||||
}
|
||||
|
||||
// Write mime_db contents to file
|
||||
String ede_mailcap = user_config_dir() + E_DIR_SEPARATOR_STR"ede"E_DIR_SEPARATOR_STR"mailcap";
|
||||
FILE *fp = fopen(ede_mailcap.c_str(),"w");
|
||||
|
||||
it = mime_db.begin();
|
||||
list<mime_db_struct>::iterator it_end = mime_db.end();
|
||||
while (it != it_end) {
|
||||
char buffer[BUFLEN];
|
||||
snprintf(buffer, BUFLEN-1, "%s; %s", (*it).type.c_str(), (*it).viewcmd.c_str());
|
||||
if ((*it).createcmd != "") {
|
||||
strncat(buffer, "; compose=", BUFLEN-1-strlen(buffer));
|
||||
strncat(buffer, (*it).createcmd.c_str(), BUFLEN-1-strlen(buffer));
|
||||
}
|
||||
if ((*it).editcmd != "") {
|
||||
strncat(buffer, "; edit=", BUFLEN-1-strlen(buffer));
|
||||
strncat(buffer, (*it).editcmd.c_str(), BUFLEN-1-strlen(buffer));
|
||||
}
|
||||
if ((*it).printcmd != "") {
|
||||
strncat(buffer, "; print=", BUFLEN-1-strlen(buffer));
|
||||
strncat(buffer, (*it).printcmd.c_str(), BUFLEN-1-strlen(buffer));
|
||||
}
|
||||
if (strlen(buffer)>BUFLEN-2)
|
||||
// Even if line is too long, we don't want to mangle the next one
|
||||
buffer[strlen(buffer)-1]='\n';
|
||||
else
|
||||
strcat(buffer, "\n");
|
||||
fputs(buffer, fp);
|
||||
++it;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user