summaryrefslogtreecommitdiff
path: root/src/lib/ecore_cocoa/ecore_cocoa_cnp.m
blob: fabfc8c5a55312f40cbc8626dea21d33618731b5 (plain)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eina.h>
#import <Cocoa/Cocoa.h>
#import "ecore_cocoa_window.h"
#include "ecore_cocoa_private.h"
#import "ecore_cocoa_app.h"

EAPI Eina_Bool
ecore_cocoa_clipboard_set(const void *data,
                          int size,
                          const char *raw_mime_type)
{
   NSMutableArray *objects;
   NSString *str = nil;
   BOOL ok = YES;
   NSString *mime_type = [NSString stringWithUTF8String:raw_mime_type];

   objects = [[NSMutableArray alloc] init];
   if ([mime_type hasPrefix:@"text/"])
     {
        str = [[NSString alloc] initWithBytes: data
                                       length: size
                                     encoding: NSUTF8StringEncoding];
        if (str)
          [objects addObject: str];
     }
   else
     {
        ERR("Mimetype %s is not handled yet", raw_mime_type);
     }

   /* Write to pasteboard */
   if ([objects count] > 0)
     {
        NSPasteboard *pb;

        pb = [NSPasteboard generalPasteboard];
        [pb clearContents];
        ok = [pb writeObjects: objects];
        [objects removeAllObjects];
     }

   return (ok) ? EINA_TRUE : EINA_FALSE;
}

EAPI Eina_Bool
ecore_cocoa_clipboard_exists(void)
{
   NSDictionary *options;
   NSPasteboard *pb;
   NSArray *items;
   NSMutableArray *classes;

   classes = [[NSMutableArray alloc] init];
   [classes addObject: [NSString class]]; // we only support strings for now
   pb = [NSPasteboard generalPasteboard];
   options = [NSDictionary dictionary];
   return [pb canReadItemWithDataConformingToTypes: classes];
}

EAPI void *
ecore_cocoa_clipboard_get(int *size,
                          const char *raw_mime_type)
{
   NSMutableArray *classes;
   void *data = NULL;
   NSDictionary *options;
   NSPasteboard *pb;
   NSArray *items;
   unsigned int len;
   BOOL string_class = NO;
   NSString *mime_type = [NSString stringWithUTF8String:raw_mime_type];

   classes = [[NSMutableArray alloc] init];

   if ([mime_type hasPrefix:@"text/"])
     {
        string_class = YES;
        [classes addObject: [NSString class]];
     }
   else
     {
        ERR("Mimetype %s is not handled yet", raw_mime_type);
        goto fail;
     }

   if ([classes count] <= 0)
     {
        ERR("No registered classes... got nothing from pasteboard");
        goto fail;
     }

   pb = [NSPasteboard generalPasteboard];
   options = [NSDictionary dictionary];
   items = [pb readObjectsForClasses: classes
                             options: options];
   if (!items)
     {
        ERR("No items in the clipboard");
        goto remove_fail;
     }
   if ([items count] != 1)
     {
        ERR("%lu items in pasteboard. Only one at the time can be handled",
            [items count]);
        goto fail;
     }

   if (string_class)
     {
        NSString *str = [items objectAtIndex: 0];
        data = (void *)[str UTF8String];
        len = [str lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
        data = eina_strndup((const char *)data, len);

        if (EINA_UNLIKELY(!data))
          {
             CRI("Failed to strndup() string \"%s\" (len: %u)",
                 (const char *)data, len);
             goto remove_fail;
          }

#if 0
        if (type & ECORE_COCOA_CNP_TYPE_MARKUP)
          {
             char *markup;
             markup = evas_textblock_text_utf8_to_markup(NULL, data);
             free(data);
             data = markup;
             if (EINA_UNLIKELY(!data))
               {
                  CRI("Failed to retrieve markup from UTF8");
                  goto remove_fail;
               }
             len = strlen(markup);
          }
#endif
     }

   if (!data)
     {
        ERR("No types retrieved!");
        goto remove_fail;
     }

   [classes removeAllObjects];

   if (size) *size = len;
   return data;

remove_fail:
   [classes removeAllObjects];
fail:
   if (size) *size = 0;
   return NULL;
}

EAPI void
ecore_cocoa_clipboard_clear(void)
{
   [[NSPasteboard generalPasteboard] clearContents];
}