1 /* 2 * Copyright (C) 2006 Zack Rusin <zack (at) kde.org> 3 * Copyright (C) 2007 Ryan Leavengood <leavengood (at) gmail.com> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "Pasteboard.h" 29 30 #include "DocumentFragment.h" 31 #include "Editor.h" 32 #include "Frame.h" 33 #include "KURL.h" 34 #include "NotImplemented.h" 35 #include "markup.h" 36 #include <support/Locker.h> 37 #include <Clipboard.h> 38 #include <Message.h> 39 #include <String.h> 40 41 42 namespace WebCore { 43 44 Pasteboard::Pasteboard() 45 { 46 } 47 48 Pasteboard* Pasteboard::generalPasteboard() 49 { 50 static Pasteboard* pasteboard = new Pasteboard(); 51 return pasteboard; 52 } 53 54 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame) 55 { 56 BClipboard clipboard("WebKit"); 57 if (!clipboard.Lock()) 58 return; 59 60 clipboard.Clear(); 61 BMessage* data = clipboard.Data(); 62 if (!data) 63 return; 64 65 data->AddString("text/plain", BString(frame->selectedText())); 66 clipboard.Commit(); 67 68 clipboard.Unlock(); 69 } 70 71 void Pasteboard::writePlainText(const String& text) 72 { 73 BClipboard clipboard("WebKit"); 74 if (!clipboard.Lock()) 75 return; 76 77 clipboard.Clear(); 78 BMessage* data = clipboard.Data(); 79 if (!data) 80 return; 81 82 data->AddString("text/plain", BString(text)); 83 clipboard.Commit(); 84 85 clipboard.Unlock(); 86 } 87 88 bool Pasteboard::canSmartReplace() 89 { 90 notImplemented(); 91 return false; 92 } 93 94 String Pasteboard::plainText(Frame* frame) 95 { 96 BClipboard clipboard("WebKit"); 97 if (!clipboard.Lock()) 98 return String(); 99 100 BMessage* data = clipboard.Data(); 101 if (!data) 102 return String(); 103 104 BString string; 105 data->FindString("text/plain", &string); 106 107 clipboard.Unlock(); 108 109 return string; 110 } 111 112 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, 113 bool allowPlainText, bool& chosePlainText) 114 { 115 notImplemented(); 116 return 0; 117 } 118 119 void Pasteboard::writeURL(const KURL& url, const String&, Frame*) 120 { 121 BClipboard clipboard("WebKit"); 122 if (!clipboard.Lock()) 123 return; 124 125 clipboard.Clear(); 126 127 BMessage* data = clipboard.Data(); 128 if (!data) 129 return; 130 131 data->AddString("text/plain", url.string()); 132 clipboard.Commit(); 133 134 clipboard.Unlock(); 135 } 136 137 void Pasteboard::writeImage(Node*, const KURL&, const String&) 138 { 139 notImplemented(); 140 } 141 142 void Pasteboard::clear() 143 { 144 BClipboard clipboard("WebKit"); 145 if (!clipboard.Lock()) 146 return; 147 148 clipboard.Clear(); 149 clipboard.Commit(); 150 151 clipboard.Unlock(); 152 } 153 154 } // namespace WebCore 155 156