• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#import "WebURLsWithTitles.h"
30
31#import <WebKit/WebNSURLExtras.h>
32#import <WebKit/WebKitNSStringExtras.h>
33
34@implementation WebURLsWithTitles
35
36+ (NSArray *)arrayWithIFURLsWithTitlesPboardType
37{
38    // Make a canned array so we don't construct it on the fly over and over.
39    static NSArray *cannedArray = nil;
40
41    if (cannedArray == nil) {
42        cannedArray = [[NSArray arrayWithObject:WebURLsWithTitlesPboardType] retain];
43    }
44
45    return cannedArray;
46}
47
48+(void)writeURLs:(NSArray *)URLs andTitles:(NSArray *)titles toPasteboard:(NSPasteboard *)pasteboard
49{
50    NSMutableArray *URLStrings;
51    NSMutableArray *titlesOrEmptyStrings;
52    unsigned index, count;
53
54    count = [URLs count];
55    if (count == 0) {
56        return;
57    }
58
59    if ([pasteboard availableTypeFromArray:[self arrayWithIFURLsWithTitlesPboardType]] == nil) {
60        return;
61    }
62
63    if (count != [titles count]) {
64        titles = nil;
65    }
66
67    URLStrings = [NSMutableArray arrayWithCapacity:count];
68    titlesOrEmptyStrings = [NSMutableArray arrayWithCapacity:count];
69    for (index = 0; index < count; ++index) {
70        [URLStrings addObject:[[URLs objectAtIndex:index] _web_originalDataAsString]];
71        [titlesOrEmptyStrings addObject:(titles == nil) ? @"" : [[titles objectAtIndex:index] _webkit_stringByTrimmingWhitespace]];
72    }
73
74    [pasteboard setPropertyList:[NSArray arrayWithObjects:URLStrings, titlesOrEmptyStrings, nil]
75                        forType:WebURLsWithTitlesPboardType];
76}
77
78+(NSArray *)titlesFromPasteboard:(NSPasteboard *)pasteboard
79{
80    if ([pasteboard availableTypeFromArray:[self arrayWithIFURLsWithTitlesPboardType]] == nil) {
81        return nil;
82    }
83
84    return [[pasteboard propertyListForType:WebURLsWithTitlesPboardType] objectAtIndex:1];
85}
86
87+(NSArray *)URLsFromPasteboard:(NSPasteboard *)pasteboard
88{
89    NSArray *URLStrings;
90    NSMutableArray *URLs;
91    unsigned index, count;
92
93    if ([pasteboard availableTypeFromArray:[self arrayWithIFURLsWithTitlesPboardType]] == nil) {
94        return nil;
95    }
96
97    URLStrings = [[pasteboard propertyListForType:WebURLsWithTitlesPboardType] objectAtIndex:0];
98    count = [URLStrings count];
99    URLs = [NSMutableArray arrayWithCapacity:count];
100    for (index = 0; index < count; ++index) {
101        [URLs addObject:[NSURL _web_URLWithDataAsString:[URLStrings objectAtIndex:index]]];
102    }
103
104    return URLs;
105}
106
107@end
108