Merge branch '2605_fish_get_fix'
[midnight-commander.git] / src / clipboard.c
blob3ed3254aabc256b32a9ee7d0c7d77dfe20889e7c
1 /*
2 Util for external clipboard.
4 Copyright (C) 2009 The Free Software Foundation, Inc.
6 Written by:
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,
24 MA 02110-1301, USA.
27 #include <config.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include "lib/global.h"
33 #include "lib/fileloc.h"
34 #include "lib/mcconfig.h"
35 #include "lib/util.h"
36 #include "lib/event.h"
38 #include "lib/vfs/vfs.h"
40 #include "main.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 /* --------------------------------------------------------------------------------------------- */
64 /* event callback */
65 gboolean
66 clipboard_file_to_ext_clip (const gchar * event_group_name, const gchar * event_name,
67 gpointer init_data, gpointer data)
69 char *tmp, *cmd;
70 int res = 0;
71 const char *d = getenv ("DISPLAY");
73 (void) event_group_name;
74 (void) event_name;
75 (void) init_data;
76 (void) data;
78 if (d == NULL || clipboard_store_path == NULL || clipboard_store_path[0] == '\0')
79 return TRUE;
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);
84 if (cmd != NULL)
85 res = my_system (EXECUTE_AS_SHELL, shell, cmd);
87 g_free (cmd);
88 g_free (tmp);
89 return TRUE;
92 /* --------------------------------------------------------------------------------------------- */
94 /* event callback */
95 gboolean
96 clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * event_name,
97 gpointer init_data, gpointer data)
99 char *tmp, *cmd;
100 int res = 0;
101 const char *d = getenv ("DISPLAY");
103 (void) event_group_name;
104 (void) event_name;
105 (void) init_data;
106 (void) data;
108 if (d == NULL || clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0')
109 return TRUE;
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);
114 if (cmd != NULL)
115 res = my_system (EXECUTE_AS_SHELL, shell, cmd);
117 g_free (cmd);
118 g_free (tmp);
119 return TRUE;
122 /* --------------------------------------------------------------------------------------------- */
124 /* event callback */
125 gboolean
126 clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name,
127 gpointer init_data, gpointer data)
129 int file;
130 char *fname = NULL;
131 ssize_t ret;
132 size_t str_len;
133 const char *text = (const char *) data;
135 (void) event_group_name;
136 (void) event_name;
137 (void) init_data;
139 if (text == NULL)
140 return FALSE;
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);
145 g_free (fname);
147 if (file == -1)
148 return TRUE;
150 str_len = strlen (text);
151 ret = mc_write (file, (char *) text, str_len);
152 mc_close (file);
153 return TRUE;
156 /* --------------------------------------------------------------------------------------------- */
158 /* event callback */
159 gboolean
160 clipboard_text_from_file (const gchar * event_group_name, const gchar * event_name,
161 gpointer init_data, gpointer data)
163 char buf[BUF_LARGE];
164 FILE *f;
165 char *fname = NULL;
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;
170 (void) event_name;
171 (void) init_data;
173 fname = g_build_filename (mc_config_get_cache_path (), EDIT_CLIP_FILE, NULL);
174 f = fopen (fname, "r");
175 g_free (fname);
177 if (f == NULL)
179 event_data->ret = FALSE;
180 return TRUE;
183 *(event_data->text) = NULL;
185 while (fgets (buf, sizeof (buf), f))
187 size_t len;
189 len = strlen (buf);
190 if (len > 0)
192 if (buf[len - 1] == '\n')
193 buf[len - 1] = '\0';
195 if (first)
197 first = FALSE;
198 *(event_data->text) = g_strdup (buf);
200 else
202 /* remove \n on EOL */
203 char *tmp;
205 tmp = g_strconcat (*(event_data->text), " ", buf, (char *) NULL);
206 g_free (*(event_data->text));
207 *(event_data->text) = tmp;
212 fclose (f);
213 event_data->ret = (*(event_data->text) != NULL);
214 return TRUE;
217 /* --------------------------------------------------------------------------------------------- */