1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* 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 Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* Holger Macht <hmacht@suse.de>
* based on work by Sankar P <psankar@novell.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <mail/em-config.h>
#include <mail/em-composer-utils.h>
#include <e-msg-composer.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <gdk/gdkkeysyms.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#include <stdlib.h>
#include <string.h>
#define d(x)
gboolean e_plugin_ui_init (GtkUIManager *manager,
EMsgComposer *composer);
GtkWidget * e_plugin_lib_get_configure_widget
(EPlugin *epl);
static void ee_editor_command_changed
(GtkWidget *textbox);
static void ee_editor_immediate_launch_changed
(GtkWidget *checkbox);
static gboolean editor_running (void);
static gboolean key_press_cb (GtkWidget *widget,
GdkEventKey *event,
EMsgComposer *composer);
/* used to track when the external editor is active */
static GThread *editor_thread;
gint e_plugin_lib_enable (EPlugin *ep, gint enable);
gint
e_plugin_lib_enable (EPlugin *ep,
gint enable)
{
return 0;
}
void
ee_editor_command_changed (GtkWidget *textbox)
{
const gchar *editor;
GSettings *settings;
editor = gtk_entry_get_text (GTK_ENTRY (textbox));
d (printf ("\n\aeditor is : [%s] \n\a", editor));
/* GSettings access for every key-press. Sucky ? */
settings = e_util_ref_settings ("org.gnome.evolution.plugin.external-editor");
g_settings_set_string (settings, "command", editor);
g_object_unref (settings);
}
void
ee_editor_immediate_launch_changed (GtkWidget *checkbox)
{
gboolean immediately;
GSettings *settings;
immediately = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbox));
d (printf ("\n\aimmediate launch is : [%d] \n\a", immediately));
settings = e_util_ref_settings ("org.gnome.evolution.plugin.external-editor");
g_settings_set_boolean (settings, "launch-on-key-press", immediately);
g_object_unref (settings);
}
GtkWidget *
e_plugin_lib_get_configure_widget (EPlugin *epl)
{
GtkWidget *vbox, *textbox, *label, *help;
GtkWidget *checkbox;
GSettings *settings;
gchar *editor;
gboolean checked;
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
textbox = gtk_entry_new ();
label = gtk_label_new (_("Command to be executed to launch the editor: "));
help = gtk_label_new (_("For XEmacs use \"xemacs\"\nFor Vim use \"gvim -f\""));
settings = e_util_ref_settings ("org.gnome.evolution.plugin.external-editor");
editor = g_settings_get_string (settings, "command");
if (editor) {
gtk_entry_set_text (GTK_ENTRY (textbox), editor);
g_free (editor);
}
checkbox = gtk_check_button_new_with_mnemonic (
_("_Automatically launch when a new mail is edited"));
checked = g_settings_get_boolean (settings, "launch-on-key-press");
if (checked)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox), TRUE);
g_object_unref (settings);
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), textbox, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), help, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), checkbox, FALSE, FALSE, 0);
g_signal_connect (
textbox, "changed",
G_CALLBACK (ee_editor_command_changed), textbox);
g_signal_connect (
checkbox, "toggled",
G_CALLBACK (ee_editor_immediate_launch_changed), checkbox);
gtk_widget_show_all (vbox);
return vbox;
}
static void
enable_disable_composer (EMsgComposer *composer,
gboolean enable)
{
EHTMLEditor *editor;
EContentEditor *cnt_editor;
GtkAction *action;
GtkActionGroup *action_group;
g_return_if_fail (E_IS_MSG_COMPOSER (composer));
editor = e_msg_composer_get_editor (composer);
cnt_editor = e_html_editor_get_content_editor (editor);
e_content_editor_set_editable (cnt_editor, enable);
action = E_HTML_EDITOR_ACTION_EDIT_MENU (editor);
gtk_action_set_sensitive (action, enable);
action = E_HTML_EDITOR_ACTION_FORMAT_MENU (editor);
gtk_action_set_sensitive (action, enable);
action = E_HTML_EDITOR_ACTION_INSERT_MENU (editor);
gtk_action_set_sensitive (action, enable);
action_group = e_html_editor_get_action_group (editor, "composer");
gtk_action_group_set_sensitive (action_group, enable);
}
static void
enable_composer (EMsgComposer *composer)
{
enable_disable_composer (composer, TRUE);
}
static void
disable_composer (EMsgComposer *composer)
{
enable_disable_composer (composer, FALSE);
}
static gboolean
enable_composer_idle (gpointer user_data)
{
EMsgComposer *composer = user_data;
g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), FALSE);
enable_composer (composer);
g_object_unref (composer);
return FALSE;
}
struct ExternalEditorData {
EMsgComposer *composer;
gchar *content;
gint cursor_position, cursor_offset;
};
/* needed because the new thread needs to call g_idle_add () */
static gboolean
update_composer_text (gpointer user_data)
{
struct ExternalEditorData *eed = user_data;
EHTMLEditor *editor;
EContentEditor *cnt_editor;
g_return_val_if_fail (eed != NULL, FALSE);
g_return_val_if_fail (E_IS_MSG_COMPOSER (eed->composer), FALSE);
editor = e_msg_composer_get_editor (eed->composer);
cnt_editor = e_html_editor_get_content_editor (editor);
e_msg_composer_set_body_text (eed->composer, eed->content, FALSE);
enable_composer (eed->composer);
e_content_editor_set_changed (cnt_editor, TRUE);
g_clear_object (&eed->composer);
g_free (eed->content);
g_free (eed);
return FALSE;
}
struct run_error_dialog_data
{
EMsgComposer *composer;
const gchar *text;
};
/* needed because the new thread needs to call g_idle_add () */
static gboolean
run_error_dialog (struct run_error_dialog_data *data)
{
g_return_val_if_fail (data != NULL, FALSE);
e_alert_run_dialog_for_args (GTK_WINDOW (data->composer), data->text, NULL);
enable_composer (data->composer);
g_clear_object (&data->composer);
g_free (data);
return FALSE;
}
static gint
numlines (const gchar *text,
gint pos)
{
gint ctr = 0;
gint lineno = 0;
while (text && *text && ctr <= pos) {
if (*text == '\n')
lineno++;
text++;
ctr++;
}
if (lineno > 0) {
lineno++;
}
return lineno;
}
static gboolean external_editor_running = FALSE;
static GMutex external_editor_running_lock;
static gpointer
external_editor_thread (gpointer user_data)
{
struct ExternalEditorData *eed = user_data;
gchar *filename = NULL;
gint status = 0;
GSettings *settings;
gchar *editor_cmd_line = NULL, *editor_cmd = NULL;
gint fd;
g_return_val_if_fail (eed != NULL, NULL);
g_return_val_if_fail (E_IS_MSG_COMPOSER (eed->composer), NULL);
/* prefix temp files with evo so .*vimrc can be setup to recognize them */
fd = g_file_open_tmp ("evoXXXXXX", &filename, NULL);
if (fd > 0) {
close (fd);
d (printf ("\n\aTemporary-file Name is : [%s] \n\a", filename));
/* Push the text (if there is one) from the composer to the file */
if (eed->content && *eed->content)
g_file_set_contents (filename, eed->content, strlen (eed->content), NULL);
} else {
struct run_error_dialog_data *data;
data = g_new0 (struct run_error_dialog_data, 1);
data->composer = g_object_ref (eed->composer);
data->text = "org.gnome.evolution.plugins.external-editor:no-temp-file";
g_warning ("Temporary file fd is null");
/* run_error_dialog also calls enable_composer */
g_idle_add ((GSourceFunc) run_error_dialog, data);
goto finished;
}
settings = e_util_ref_settings ("org.gnome.evolution.plugin.external-editor");
editor_cmd = g_settings_get_string (settings, "command");
if (!editor_cmd) {
if (!(editor_cmd = g_strdup (g_getenv ("EDITOR"))) )
/* Make gedit the default external editor,
* if the default schemas are not installed
* and no $EDITOR is set. */
editor_cmd = g_strdup ("gedit");
}
g_object_unref (settings);
if (g_strrstr (editor_cmd, "vim") != NULL &&
eed->cursor_position > 0) {
gchar *tmp = editor_cmd;
gint lineno;
gboolean set_nofork;
set_nofork = g_strrstr (editor_cmd, "gvim") != NULL;
/* calculate the line number that the cursor is in */
lineno = numlines (eed->content, eed->cursor_position);
/* Increment by 1 so that entering vim insert mode places you
* in the same entry position you were at in the html. */
editor_cmd = g_strdup_printf (
"%s \"+call cursor(%d,%d)\"%s%s",
tmp, lineno, eed->cursor_offset + 1,
set_nofork ? " " : "",
set_nofork ? "--nofork" : "");
g_free (tmp);
}
editor_cmd_line = g_strconcat (editor_cmd, " ", filename, NULL);
if (!g_spawn_command_line_sync (editor_cmd_line, NULL, NULL, &status, NULL)) {
struct run_error_dialog_data *data;
g_warning ("Unable to launch %s: ", editor_cmd_line);
data = g_new0 (struct run_error_dialog_data, 1);
data->composer = g_object_ref (eed->composer);
data->text = "org.gnome.evolution.plugins.external-editor:editor-not-launchable";
/* run_error_dialog also calls enable_composer */
g_idle_add ((GSourceFunc) run_error_dialog, data);
g_free (filename);
g_free (editor_cmd_line);
g_free (editor_cmd);
goto finished;
}
g_free (editor_cmd_line);
g_free (editor_cmd);
#ifdef HAVE_SYS_WAIT_H
if (WEXITSTATUS (status) != 0) {
#else
if (status) {
#endif
d (printf ("\n\nsome problem here with external editor\n\n"));
g_idle_add (enable_composer_idle, g_object_ref (eed->composer));
goto finished;
} else {
gchar *buf;
if (g_file_get_contents (filename, &buf, NULL, NULL)) {
struct ExternalEditorData *eed2;
eed2 = g_new0 (struct ExternalEditorData, 1);
eed2->composer = g_object_ref (eed->composer);
eed2->content = camel_text_to_html (buf, CAMEL_MIME_FILTER_TOHTML_PRE, 0);
g_idle_add ((GSourceFunc) update_composer_text, eed2);
/* We no longer need that temporary file */
if (g_remove (filename) == -1)
g_warning (
"%s: Failed to remove file '%s': %s",
G_STRFUNC, filename, g_strerror (errno));
g_free (filename);
}
}
finished:
g_mutex_lock (&external_editor_running_lock);
external_editor_running = FALSE;
g_mutex_unlock (&external_editor_running_lock);
g_clear_object (&eed->composer);
g_free (eed->content);
g_free (eed);
return NULL;
}
static void launch_editor (GtkAction *action, EMsgComposer *composer)
{
struct ExternalEditorData *eed;
EHTMLEditor *editor;
EContentEditor *cnt_editor;
d (printf ("\n\nexternal_editor plugin is launched \n\n"));
if (editor_running ()) {
d (printf ("not opening editor, because it's still running\n"));
return;
}
editor = e_msg_composer_get_editor (composer);
cnt_editor = e_html_editor_get_content_editor (editor);
e_content_editor_clear_undo_redo_history (cnt_editor);
disable_composer (composer);
g_mutex_lock (&external_editor_running_lock);
external_editor_running = TRUE;
g_mutex_unlock (&external_editor_running_lock);
eed = g_new0 (struct ExternalEditorData, 1);
eed->composer = g_object_ref (composer);
eed->content = e_content_editor_get_content (cnt_editor,
E_CONTENT_EDITOR_GET_TEXT_PLAIN |
E_CONTENT_EDITOR_GET_PROCESSED,
NULL, NULL);
eed->cursor_position = e_content_editor_get_caret_position (cnt_editor);
if (eed->cursor_position > 0)
eed->cursor_offset = e_content_editor_get_caret_offset (cnt_editor);
editor_thread = g_thread_new (NULL, external_editor_thread, eed);
g_thread_unref (editor_thread);
}
static GtkActionEntry entries[] = {
{ "ExternalEditor",
NULL,
N_("Compose in External Editor"),
"<Shift><Control>e",
N_("Compose in External Editor"),
G_CALLBACK (launch_editor) }
};
static gboolean
key_press_cb (GtkWidget *widget,
GdkEventKey *event,
EMsgComposer *composer)
{
GSettings *settings;
gboolean immediately;
/* we don't want to start the editor on any modifier keys */
switch (event->keyval) {
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Super_L:
case GDK_KEY_Super_R:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
return FALSE;
default:
break;
}
settings = e_util_ref_settings ("org.gnome.evolution.plugin.external-editor");
immediately = g_settings_get_boolean (settings, "launch-on-key-press");
g_object_unref (settings);
if (!immediately)
return FALSE;
launch_editor (NULL, composer);
return TRUE;
}
static gboolean
editor_running (void)
{
gboolean running;
g_mutex_lock (&external_editor_running_lock);
running = external_editor_running;
g_mutex_unlock (&external_editor_running_lock);
return running;
}
static gboolean
delete_cb (GtkWidget *widget,
EMsgComposer *composer)
{
if (editor_running ()) {
e_alert_run_dialog_for_args (
NULL, "org.gnome.evolution.plugins."
"external-editor:editor-still-running", NULL);
return TRUE;
}
return FALSE;
}
gboolean
e_plugin_ui_init (GtkUIManager *manager,
EMsgComposer *composer)
{
EHTMLEditor *editor;
EContentEditor *cnt_editor;
editor = e_msg_composer_get_editor (composer);
cnt_editor = e_html_editor_get_content_editor (editor);
/* Add actions to the "composer" action group. */
gtk_action_group_add_actions (
e_html_editor_get_action_group (editor, "composer"),
entries, G_N_ELEMENTS (entries), composer);
g_signal_connect (
cnt_editor, "key_press_event",
G_CALLBACK (key_press_cb), composer);
g_signal_connect (
cnt_editor, "delete-event",
G_CALLBACK (delete_cb), composer);
return TRUE;
}
|