2 Util for external clipboard.
4 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Ilia Maslakon <il.smind@gmail.com>, 2010.
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software; you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be
17 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
18 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 #include "lib/global.h"
33 #include "lib/fileloc.h"
34 #include "lib/mcconfig.h"
36 #include "lib/event.h"
38 #include "lib/vfs/vfs.h"
41 #include "src/execute.h"
43 #include "clipboard.h"
45 /*** global variables ****************************************************************************/
47 /* path to X clipboard utility */
48 char *clipboard_store_path
= NULL
;
49 char *clipboard_paste_path
= NULL
;
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 /* --------------------------------------------------------------------------------------------- */
61 /*** public functions ****************************************************************************/
62 /* --------------------------------------------------------------------------------------------- */
66 clipboard_file_to_ext_clip (const gchar
* event_group_name
, const gchar
* event_name
,
67 gpointer init_data
, gpointer data
)
71 const char *d
= getenv ("DISPLAY");
73 (void) event_group_name
;
78 if (d
== NULL
|| clipboard_store_path
== NULL
|| clipboard_store_path
[0] == '\0')
81 tmp
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_CLIP_FILE
);
82 cmd
= g_strconcat (clipboard_store_path
, " ", tmp
, " 2>/dev/null", (char *) NULL
);
85 res
= my_system (EXECUTE_AS_SHELL
, shell
, cmd
);
92 /* --------------------------------------------------------------------------------------------- */
96 clipboard_file_from_ext_clip (const gchar
* event_group_name
, const gchar
* event_name
,
97 gpointer init_data
, gpointer data
)
101 const char *d
= getenv ("DISPLAY");
103 (void) event_group_name
;
108 if (d
== NULL
|| clipboard_paste_path
== NULL
|| clipboard_paste_path
[0] == '\0')
111 tmp
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_CLIP_FILE
);
112 cmd
= g_strconcat (clipboard_paste_path
, " > ", tmp
, " 2>/dev/null", (char *) NULL
);
115 res
= my_system (EXECUTE_AS_SHELL
, shell
, cmd
);
122 /* --------------------------------------------------------------------------------------------- */
126 clipboard_text_to_file (const gchar
* event_group_name
, const gchar
* event_name
,
127 gpointer init_data
, gpointer data
)
133 const char *text
= (const char *) data
;
135 (void) event_group_name
;
142 fname
= g_build_filename (mc_config_get_cache_path (), EDIT_CLIP_FILE
, NULL
);
143 file
= mc_open (fname
, O_CREAT
| O_WRONLY
| O_TRUNC
,
144 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
150 str_len
= strlen (text
);
151 ret
= mc_write (file
, (char *) text
, str_len
);
156 /* --------------------------------------------------------------------------------------------- */
160 clipboard_text_from_file (const gchar
* event_group_name
, const gchar
* event_name
,
161 gpointer init_data
, gpointer data
)
166 gboolean first
= TRUE
;
167 ev_clipboard_text_from_file_t
*event_data
= (ev_clipboard_text_from_file_t
*) data
;
169 (void) event_group_name
;
173 fname
= g_build_filename (mc_config_get_cache_path (), EDIT_CLIP_FILE
, NULL
);
174 f
= fopen (fname
, "r");
179 event_data
->ret
= FALSE
;
183 *(event_data
->text
) = NULL
;
185 while (fgets (buf
, sizeof (buf
), f
))
192 if (buf
[len
- 1] == '\n')
198 *(event_data
->text
) = g_strdup (buf
);
202 /* remove \n on EOL */
205 tmp
= g_strconcat (*(event_data
->text
), " ", buf
, (char *) NULL
);
206 g_free (*(event_data
->text
));
207 *(event_data
->text
) = tmp
;
213 event_data
->ret
= (*(event_data
->text
) != NULL
);
217 /* --------------------------------------------------------------------------------------------- */