2 Util for external clipboard.
4 Copyright (C) 2009, 2011
5 The Free Software Foundation, Inc.
8 Ilia Maslakov <il.smind@gmail.com>, 2010.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "lib/global.h"
32 #include "lib/fileloc.h"
33 #include "lib/mcconfig.h"
35 #include "lib/event.h"
37 #include "lib/vfs/vfs.h"
39 #include "src/execute.h"
41 #include "clipboard.h"
43 /*** global variables ****************************************************************************/
45 /* path to X clipboard utility */
46 char *clipboard_store_path
= NULL
;
47 char *clipboard_paste_path
= NULL
;
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
58 /* --------------------------------------------------------------------------------------------- */
59 /*** public functions ****************************************************************************/
60 /* --------------------------------------------------------------------------------------------- */
64 clipboard_file_to_ext_clip (const gchar
* event_group_name
, const gchar
* event_name
,
65 gpointer init_data
, gpointer data
)
68 const char *d
= getenv ("DISPLAY");
70 (void) event_group_name
;
75 if (d
== NULL
|| clipboard_store_path
== NULL
|| clipboard_store_path
[0] == '\0')
78 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
79 cmd
= g_strconcat (clipboard_store_path
, " ", tmp
, " 2>/dev/null", (char *) NULL
);
82 my_system (EXECUTE_AS_SHELL
, mc_global
.tty
.shell
, cmd
);
89 /* --------------------------------------------------------------------------------------------- */
93 clipboard_file_from_ext_clip (const gchar
* event_group_name
, const gchar
* event_name
,
94 gpointer init_data
, gpointer data
)
97 const char *d
= getenv ("DISPLAY");
99 (void) event_group_name
;
104 if (d
== NULL
|| clipboard_paste_path
== NULL
|| clipboard_paste_path
[0] == '\0')
107 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
108 cmd
= g_strconcat (clipboard_paste_path
, " > ", tmp
, " 2>/dev/null", (char *) NULL
);
111 my_system (EXECUTE_AS_SHELL
, mc_global
.tty
.shell
, cmd
);
118 /* --------------------------------------------------------------------------------------------- */
122 clipboard_text_to_file (const gchar
* event_group_name
, const gchar
* event_name
,
123 gpointer init_data
, gpointer data
)
126 vfs_path_t
*fname_vpath
= NULL
;
128 const char *text
= (const char *) data
;
130 (void) event_group_name
;
137 fname_vpath
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
138 file
= mc_open (fname_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
139 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
140 vfs_path_free (fname_vpath
);
145 str_len
= strlen (text
);
149 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
= mc_config_get_full_path (EDIT_CLIP_FILE
);
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 /* --------------------------------------------------------------------------------------------- */