summaryrefslogtreecommitdiff
path: root/Sources/Data/OwlPasteboardDataDevice.m
blob: 7a514eaba8351ec8bf481deaab9c5d36bf0b2456 (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
/* This file is part of Owl.
 *
 * Copyright © 2019-2021 Sergey Bugaev <bugaevc@gmail.com>
 *
 * Owl is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * Owl is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Owl.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "OwlPasteboardDataDevice.h"
#import "OwlPasteboardDataOffer.h"
#import "OwlPasteboardDataSource.h"
#import "OwlServer.h"
#import "OwlSelection.h"


@implementation OwlPasteboardDataDevice

// Publish a fresh data source representing our pasteboard
// as the selection content.
- (void) publishDataSource {
    OwlPasteboardDataSource *dataSource = [OwlPasteboardDataSource alloc];
    dataSource = [dataSource initWithPasteboard: _pasteboard];

    [_selection setDataSource: dataSource];
    [dataSource release];
    [[OwlServer sharedServer] flushClientsLater];
}

// FIXME: This is only called by our own offers when they get cancelled,
// which means we miss refreshes when we don't own the pasteboard.
- (void) pasteboardRefreshed {
    if (_ignoreRefreshes) {
        return;
    }
    // We have to do this asynchronously, otherwise NSPasteboard gets confused over
    // accessing pasteboard contents from the pasteboardChangedOwner handler, and
    // sends the same notification again and again, recursively.
    // The order argument below represents which order these delayed selectors should
    // be executed in; lower values go first. We do not care, so pass some large number.
    NSArray *modes = [NSArray arrayWithObject: NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] performSelector: @selector(publishDataSource)
                                         target: self
                                       argument: nil
                                          order: 1000
                                          modes: modes];
}

- (id) initWithPasteboard: (NSPasteboard *) pboard
                selection: (OwlSelection *) selection
{
    self = [super initWithResource: NULL];
    _pasteboard = [pboard retain];
    _selection = [selection retain];
    [selection addDataDevice: self];
    // On startup, initialize selection with pasteboard contents.
    // We assume no clients have connected yet.
    [self pasteboardRefreshed];
    return self;
}

- (void) dealloc {
    [_pasteboard release];
    [_selection removeDataDevice: self];
    [_selection release];
    [_currentOffer release];
    [super dealloc];
}

- (NSPasteboard *) pasteboard {
    return _pasteboard;
}

// Selection has changed; proxy the new contents to the pasteboard.
- (void) selectionChanged: (OwlSelection *) selection {
    OwlDataSource *dataSource = [selection dataSource];
    [_currentOffer release];
    _currentOffer = nil;

    if (dataSource == nil) {
        [_pasteboard clearContents];
        return;
    }

    // Don't proxy pasteboard contents back to the pasteboard.
    if ([dataSource isKindOfClass: [OwlPasteboardDataSource class]]) {
        return;
    }

    _ignoreRefreshes = YES;
    _currentOffer = [OwlPasteboardDataOffer alloc];
    _currentOffer = [_currentOffer initWithDataDevice: self
                                           dataSource: dataSource];
    _ignoreRefreshes = NO;
}

@end