2 * Copyright 2005, Haiku Inc.
3 * Distributed under the terms of the MIT license.
6 * Jonas Sundström, jonas@kirilla.com
7 * Axel Dörfler, axeld@pinc-software.de.
11 #include <Application.h>
12 #include <Clipboard.h>
14 #include <FindDirectory.h>
24 extern const char *__progname
;
25 static const char *kProgramName
= __progname
;
27 static int sExitValue
= EXIT_SUCCESS
;
30 class ClipboardApp
: public BApplication
{
33 virtual ~ClipboardApp(void);
35 virtual void ReadyToRun(void);
36 virtual void ArgvReceived(int32 argc
, char **argv
);
40 status_t
Load(const char *path
);
41 status_t
Save(const char *path
);
42 status_t
Copy(const char *string
);
44 status_t
Print(bool debug
);
50 ClipboardApp::ClipboardApp(void)
51 : BApplication("application/x-vnd.haiku-clipboard"),
57 ClipboardApp::~ClipboardApp(void)
63 ClipboardApp::ArgvReceived(int32 argc
, char **argv
)
65 status_t status
= B_OK
;
67 static struct option
const kLongOptions
[] = {
68 {"load", required_argument
, 0, 'l'},
69 {"save", required_argument
, 0, 's'},
70 {"copy", required_argument
, 0, 'c'},
71 {"clear", no_argument
, 0, 'r'},
72 {"stdin", no_argument
, 0, 'i'},
73 {"print", no_argument
, 0, 'p'},
74 {"debug", no_argument
, 0, 'd'},
75 {"help", no_argument
, 0, 'h'},
80 while ((c
= getopt_long(argc
, argv
, "l:s:o:c:ripdh", kLongOptions
, NULL
))
84 status
= Load(optarg
);
88 status
= Save(optarg
);
91 status
= Copy(optarg
);
100 status
= Print(false);
103 status
= Print(true);
108 // help text is printed in ReadyToRun()
117 fGotArguments
= true;
119 sExitValue
= EXIT_FAILURE
;
124 ClipboardApp::ReadyToRun(void)
126 if (fGotArguments
== false)
129 PostMessage(B_QUIT_REQUESTED
);
134 ClipboardApp::Usage(void)
136 printf("usage: %s [-olcirpd]\n"
137 " -s, --save=file\tSave clipboard to file (flattened BMessage)\n"
138 " -o\t\t\tthe same\n" // ToDo: (\"-\" for standard output/input)\n"
139 " -l, --load=file\tLoad clipboard from file (flattened BMessage)\n"
140 " -c, --copy=string\tPut the given string on the clipboard\n"
141 " -i, --stdin\t\tLoad clipboard with string from standard input\n\n"
143 " -r, --clear\t\tRemove all contents from clipboard\n\n"
145 " -p, --print\t\tPrint clipboard to standard output\n"
146 " -d, --debug\t\tPrint clipboard message to stdout\n\n"
148 " -h, --help\t\tDisplay this help and exit\n",
154 ClipboardApp::Load(const char *path
)
156 status_t status
= B_OK
;
159 status
= file
.SetTo(path
, B_READ_ONLY
);
160 if (status
!= B_OK
) {
161 fprintf(stderr
, "%s: Could not open file: %s\n", kProgramName
,
166 // get BMessage from file
167 BMessage clipFromFile
;
168 status
= clipFromFile
.Unflatten(&file
);
169 if (status
!= B_OK
) {
170 fprintf(stderr
, "%s: Could not read clip: %s\n", kProgramName
,
175 // load clip into clipboard
176 if (!be_clipboard
->Lock()) {
177 fprintf(stderr
, "%s: could not lock clipboard.\n", kProgramName
);
181 be_clipboard
->Clear();
183 BMessage
*clip
= be_clipboard
->Data();
184 *clip
= clipFromFile
;
186 status
= be_clipboard
->Commit();
187 if (status
!= B_OK
) {
188 fprintf(stderr
, "%s: could not commit data to clipboard.\n",
190 be_clipboard
->Unlock();
194 be_clipboard
->Unlock();
200 ClipboardApp::Save(const char *path
)
202 status_t status
= B_OK
;
205 status
= file
.SetTo(path
, B_WRITE_ONLY
| B_CREATE_FILE
| B_ERASE_FILE
);
206 if (status
!= B_OK
) {
207 fprintf(stderr
, "%s: Could not create file: %s\n", kProgramName
,
213 if (!be_clipboard
->Lock()) {
214 fprintf(stderr
, "%s: could not lock clipboard.\n", kProgramName
);
218 BMessage
*clip
= be_clipboard
->Data();
220 be_clipboard
->Unlock();
222 // flatten clip to file
223 status
= clip
->Flatten(&file
);
224 if (status
!= B_OK
) {
225 fprintf(stderr
, "%s: Could not write data: %s\n", kProgramName
,
235 ClipboardApp::Copy(const char *string
)
237 status_t status
= B_OK
;
240 if (string
== NULL
) {
241 // read from standard input
243 while ((c
= fgetc(stdin
)) != EOF
) {
244 inputString
+= (char)c
;
247 string
= inputString
.String();
250 // get clipboard BMessage
252 if (be_clipboard
->Lock()) {
253 be_clipboard
->Clear();
255 BMessage
*clip
= be_clipboard
->Data();
257 // add data to clipboard
258 clip
->AddData("text/plain", B_MIME_TYPE
, string
, strlen(string
));
260 status
= be_clipboard
->Commit();
261 if (status
!= B_OK
) {
262 fprintf(stderr
, "%s: could not commit data to clipboard.\n",
264 be_clipboard
->Unlock();
268 be_clipboard
->Unlock();
270 fprintf(stderr
, "%s: could not lock clipboard.\n", kProgramName
);
279 ClipboardApp::Clear(void)
281 status_t status
= B_OK
;
283 if (!be_clipboard
->Lock()) {
284 fprintf(stderr
, "%s: could not lock clipboard.\n", kProgramName
);
288 be_clipboard
->Clear();
290 status
= be_clipboard
->Commit();
291 if (status
!= B_OK
) {
292 fprintf(stderr
, "%s: could not clear clipboard.\n", kProgramName
);
293 be_clipboard
->Unlock();
297 be_clipboard
->Unlock();
303 ClipboardApp::Print(bool debug
)
305 // get clip & print contents
306 // (or the entire clip, in case of --debug)
308 if (!be_clipboard
->Lock()) {
309 fprintf(stderr
, "%s: could not lock clipboard.\n", kProgramName
);
313 BMessage
*clip
= be_clipboard
->Data();
316 clip
->PrintToStream();
318 const char * textBuffer
;
320 clip
->FindData("text/plain", B_MIME_TYPE
, (const void **)&textBuffer
,
323 if (textBuffer
!= NULL
&& textLength
> 0) {
324 BString
textString(textBuffer
, textLength
);
325 printf("%s\n", textString
.String());
329 be_clipboard
->Unlock();
340 ClipboardApp clip_app
;