diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/os_exchange_data.cc | 50 | ||||
-rw-r--r-- | app/os_exchange_data.h | 58 | ||||
-rw-r--r-- | app/os_exchange_data_provider_gtk.cc | 104 | ||||
-rw-r--r-- | app/os_exchange_data_provider_gtk.h | 21 |
4 files changed, 107 insertions, 126 deletions
diff --git a/app/os_exchange_data.cc b/app/os_exchange_data.cc index 9361466..8f647c2 100644 --- a/app/os_exchange_data.cc +++ b/app/os_exchange_data.cc @@ -32,15 +32,6 @@ void OSExchangeData::SetPickledData(CustomFormat format, const Pickle& data) { provider_->SetPickledData(format, data); } -void OSExchangeData::SetFileContents(const std::wstring& filename, - const std::string& file_contents) { - provider_->SetFileContents(filename, file_contents); -} - -void OSExchangeData::SetHtml(const std::wstring& html, const GURL& base_url) { - provider_->SetHtml(html, base_url); -} - bool OSExchangeData::GetString(std::wstring* data) const { return provider_->GetString(data); } @@ -57,15 +48,6 @@ bool OSExchangeData::GetPickledData(CustomFormat format, Pickle* data) const { return provider_->GetPickledData(format, data); } -bool OSExchangeData::GetFileContents(std::wstring* filename, - std::string* file_contents) const { - return provider_->GetFileContents(filename, file_contents); -} - -bool OSExchangeData::GetHtml(std::wstring* html, GURL* base_url) const { - return provider_->GetHtml(html, base_url); -} - bool OSExchangeData::HasString() const { return provider_->HasString(); } @@ -89,12 +71,14 @@ bool OSExchangeData::HasAllFormats( return false; if ((formats & URL) != 0 && !HasURL()) return false; +#if defined(OS_WIN) if ((formats & FILE_CONTENTS) != 0 && !provider_->HasFileContents()) return false; - if ((formats & FILE_NAME) != 0 && !provider_->HasFile()) - return false; if ((formats & HTML) != 0 && !provider_->HasHtml()) return false; +#endif + if ((formats & FILE_NAME) != 0 && !provider_->HasFile()) + return false; for (std::set<CustomFormat>::const_iterator i = custom_formats.begin(); i != custom_formats.end(); ++i) { if (!HasCustomFormat(*i)) @@ -110,12 +94,14 @@ bool OSExchangeData::HasAnyFormat( return true; if ((formats & URL) != 0 && HasURL()) return true; +#if defined(OS_WIN) if ((formats & FILE_CONTENTS) != 0 && provider_->HasFileContents()) return true; - if ((formats & FILE_NAME) != 0 && provider_->HasFile()) - return true; if ((formats & HTML) != 0 && provider_->HasHtml()) return true; +#endif + if ((formats & FILE_NAME) != 0 && provider_->HasFile()) + return true; for (std::set<CustomFormat>::const_iterator i = custom_formats.begin(); i != custom_formats.end(); ++i) { if (HasCustomFormat(*i)) @@ -123,3 +109,23 @@ bool OSExchangeData::HasAnyFormat( } return false; } + +#if defined(OS_WIN) +void OSExchangeData::SetFileContents(const std::wstring& filename, + const std::string& file_contents) { + provider_->SetFileContents(filename, file_contents); +} + +void OSExchangeData::SetHtml(const std::wstring& html, const GURL& base_url) { + provider_->SetHtml(html, base_url); +} + +bool OSExchangeData::GetFileContents(std::wstring* filename, + std::string* file_contents) const { + return provider_->GetFileContents(filename, file_contents); +} + +bool OSExchangeData::GetHtml(std::wstring* html, GURL* base_url) const { + return provider_->GetHtml(html, base_url); +} +#endif diff --git a/app/os_exchange_data.h b/app/os_exchange_data.h index a956b80..7efea43 100644 --- a/app/os_exchange_data.h +++ b/app/os_exchange_data.h @@ -32,6 +32,11 @@ class Pickle; // translating that into something the OS can understand. // /////////////////////////////////////////////////////////////////////////////// + +// NOTE: Support for html and file contents is required by TabContentViewWin. +// TabContentsViewGtk uses a different class to handle drag support that does +// not use OSExchangeData. As such, file contents and html support is only +// compiled on windows. class OSExchangeData { public: // CustomFormats are used for non-standard data types. For example, bookmark @@ -46,10 +51,12 @@ class OSExchangeData { enum Format { STRING = 1 << 0, URL = 1 << 1, - FILE_CONTENTS = 1 << 2, - FILE_NAME = 1 << 3, - PICKLED_DATA = 1 << 4, - HTML = 1 << 5 + FILE_NAME = 1 << 2, + PICKLED_DATA = 1 << 3, +#if defined(OS_WIN) + FILE_CONTENTS = 1 << 4, + HTML = 1 << 5, +#endif }; // Provider defines the platform specific part of OSExchangeData that @@ -63,25 +70,28 @@ class OSExchangeData { virtual void SetURL(const GURL& url, const std::wstring& title) = 0; virtual void SetFilename(const std::wstring& full_path) = 0; virtual void SetPickledData(CustomFormat format, const Pickle& data) = 0; - virtual void SetFileContents(const std::wstring& filename, - const std::string& file_contents) = 0; - virtual void SetHtml(const std::wstring& html, const GURL& base_url) = 0; virtual bool GetString(std::wstring* data) const = 0; virtual bool GetURLAndTitle(GURL* url, std::wstring* title) const = 0; virtual bool GetFilename(std::wstring* full_path) const = 0; virtual bool GetPickledData(CustomFormat format, Pickle* data) const = 0; - virtual bool GetFileContents(std::wstring* filename, - std::string* file_contents) const = 0; - virtual bool GetHtml(std::wstring* html, GURL* base_url) const = 0; virtual bool HasString() const = 0; virtual bool HasURL() const = 0; virtual bool HasFile() const = 0; - virtual bool HasFileContents() const = 0; - virtual bool HasHtml() const = 0; virtual bool HasCustomFormat( OSExchangeData::CustomFormat format) const = 0; + +#if defined(OS_WIN) + virtual void SetFileContents(const std::wstring& filename, + const std::string& file_contents) = 0; + virtual void SetHtml(const std::wstring& html, const GURL& base_url) = 0; + virtual bool GetFileContents(std::wstring* filename, + std::string* file_contents) const = 0; + virtual bool GetHtml(std::wstring* html, GURL* base_url) const = 0; + virtual bool HasFileContents() const = 0; + virtual bool HasHtml() const = 0; +#endif }; OSExchangeData(); @@ -111,16 +121,11 @@ class OSExchangeData { void SetString(const std::wstring& data); // A URL can have an optional title in some exchange formats. void SetURL(const GURL& url, const std::wstring& title); - // A full path to a file + // A full path to a file. + // TODO: convert to Filepath. void SetFilename(const std::wstring& full_path); // Adds pickled data of the specified format. void SetPickledData(CustomFormat format, const Pickle& data); - // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR). - void SetFileContents(const std::wstring& filename, - const std::string& file_contents); - // Adds a snippet of HTML. |html| is just raw html but this sets both - // text/html and CF_HTML. - void SetHtml(const std::wstring& html, const GURL& base_url); // These functions retrieve data of the specified type. If data exists, the // functions return and the result is in the out parameter. If the data does @@ -131,9 +136,6 @@ class OSExchangeData { // Return the path of a file, if available. bool GetFilename(std::wstring* full_path) const; bool GetPickledData(CustomFormat format, Pickle* data) const; - bool GetFileContents(std::wstring* filename, - std::string* file_contents) const; - bool GetHtml(std::wstring* html, GURL* base_url) const; // Test whether or not data of certain types is present, without actually // returning anything. @@ -152,6 +154,18 @@ class OSExchangeData { bool HasAnyFormat(int formats, const std::set<CustomFormat>& custom_formats) const; +#if defined(OS_WIN) + // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR). + void SetFileContents(const std::wstring& filename, + const std::string& file_contents); + // Adds a snippet of HTML. |html| is just raw html but this sets both + // text/html and CF_HTML. + void SetHtml(const std::wstring& html, const GURL& base_url); + bool GetFileContents(std::wstring* filename, + std::string* file_contents) const; + bool GetHtml(std::wstring* html, GURL* base_url) const; +#endif + private: // Creates the platform specific Provider. static Provider* CreateProvider(); diff --git a/app/os_exchange_data_provider_gtk.cc b/app/os_exchange_data_provider_gtk.cc index 5b32907..597a5ae 100644 --- a/app/os_exchange_data_provider_gtk.cc +++ b/app/os_exchange_data_provider_gtk.cc @@ -7,7 +7,9 @@ #include <algorithm> #include "app/gtk_dnd_util.h" +#include "base/file_path.h" #include "base/string_util.h" +#include "net/base/net_util.h" OSExchangeDataProviderGtk::OSExchangeDataProviderGtk( int known_formats, @@ -53,15 +55,8 @@ GtkTargetList* OSExchangeDataProviderGtk::GetTargetList() const { OSExchangeData::URL); } - if ((formats_ & OSExchangeData::FILE_CONTENTS) != 0) - NOTIMPLEMENTED(); - if ((formats_ & OSExchangeData::FILE_NAME) != 0) - NOTIMPLEMENTED(); - - if ((formats_ & OSExchangeData::HTML) != 0) - NOTIMPLEMENTED(); - + gtk_target_list_add_uri_targets(targets, OSExchangeData::FILE_NAME); for (PickleData::const_iterator i = pickle_data_.begin(); i != pickle_data_.end(); ++i) { @@ -101,15 +96,14 @@ void OSExchangeDataProviderGtk::WriteFormatToSelection( free(uri_array[0]); } - if ((format & OSExchangeData::FILE_CONTENTS) != 0) - NOTIMPLEMENTED(); - - if ((format & OSExchangeData::FILE_NAME) != 0) - NOTIMPLEMENTED(); - - if ((format & OSExchangeData::HTML) != 0) - NOTIMPLEMENTED(); - + if ((format & OSExchangeData::FILE_NAME) != 0) { + gchar* uri_array[2]; + uri_array[0] = + strdup(net::FilePathToFileURL(FilePath(filename_)).spec().c_str()); + uri_array[1] = NULL; + gtk_selection_data_set_uris(selection, uri_array); + free(uri_array[0]); + } if ((format & OSExchangeData::PICKLED_DATA) != 0) { for (PickleData::const_iterator i = pickle_data_.begin(); @@ -138,7 +132,7 @@ void OSExchangeDataProviderGtk::SetURL(const GURL& url, } void OSExchangeDataProviderGtk::SetFilename(const std::wstring& full_path) { - filename_ = WideToUTF16Hack(full_path); + filename_ = WideToUTF8(full_path); formats_ |= OSExchangeData::FILE_NAME; } @@ -148,21 +142,6 @@ void OSExchangeDataProviderGtk::SetPickledData(GdkAtom format, formats_ |= OSExchangeData::PICKLED_DATA; } -void OSExchangeDataProviderGtk::SetFileContents( - const std::wstring& filename, - const std::string& file_contents) { - filename_ = WideToUTF16Hack(filename); - file_contents_ = file_contents; - formats_ |= OSExchangeData::FILE_CONTENTS; -} - -void OSExchangeDataProviderGtk::SetHtml(const std::wstring& html, - const GURL& base_url) { - html_ = WideToUTF16Hack(html); - base_url_ = base_url; - formats_ |= OSExchangeData::HTML; -} - bool OSExchangeDataProviderGtk::GetString(std::wstring* data) const { if ((formats_ & OSExchangeData::STRING) == 0) return false; @@ -172,8 +151,11 @@ bool OSExchangeDataProviderGtk::GetString(std::wstring* data) const { bool OSExchangeDataProviderGtk::GetURLAndTitle(GURL* url, std::wstring* title) const { - if ((formats_ & OSExchangeData::URL) == 0) - return false; + if ((formats_ & OSExchangeData::URL) == 0) { + title->clear(); + return GetPlainTextURL(url); + } + if (!url_.is_valid()) return false; @@ -185,7 +167,7 @@ bool OSExchangeDataProviderGtk::GetURLAndTitle(GURL* url, bool OSExchangeDataProviderGtk::GetFilename(std::wstring* full_path) const { if ((formats_ & OSExchangeData::FILE_NAME) == 0) return false; - *full_path = UTF16ToWideHack(filename_); + *full_path = UTF8ToWide(filename_); return true; } @@ -199,33 +181,18 @@ bool OSExchangeDataProviderGtk::GetPickledData(GdkAtom format, return true; } -bool OSExchangeDataProviderGtk::GetFileContents( - std::wstring* filename, - std::string* file_contents) const { - if ((formats_ & OSExchangeData::FILE_CONTENTS) == 0) - return false; - *filename = UTF16ToWideHack(filename_); - *file_contents = file_contents_; - return true; -} - -bool OSExchangeDataProviderGtk::GetHtml(std::wstring* html, - GURL* base_url) const { - if ((formats_ & OSExchangeData::HTML) == 0) - return false; - *html = UTF16ToWideHack(filename_); - *base_url = base_url_; - return true; -} - bool OSExchangeDataProviderGtk::HasString() const { return (known_formats_ & OSExchangeData::STRING) != 0 || (formats_ & OSExchangeData::STRING) != 0; } bool OSExchangeDataProviderGtk::HasURL() const { - return (known_formats_ & OSExchangeData::URL) != 0 || - (formats_ & OSExchangeData::URL) != 0; + if ((known_formats_ & OSExchangeData::URL) != 0 || + (formats_ & OSExchangeData::URL) != 0) { + return true; + } + // No URL, see if we have plain text that can be parsed as a URL. + return GetPlainTextURL(NULL); } bool OSExchangeDataProviderGtk::HasFile() const { @@ -233,21 +200,24 @@ bool OSExchangeDataProviderGtk::HasFile() const { (formats_ & OSExchangeData::FILE_NAME) != 0; } -bool OSExchangeDataProviderGtk::HasFileContents() const { - return (known_formats_ & OSExchangeData::FILE_CONTENTS) != 0 || - (formats_ & OSExchangeData::FILE_CONTENTS) != 0; -} - -bool OSExchangeDataProviderGtk::HasHtml() const { - return (known_formats_ & OSExchangeData::HTML) != 0 || - (formats_ & OSExchangeData::HTML) != 0; -} - bool OSExchangeDataProviderGtk::HasCustomFormat(GdkAtom format) const { return known_custom_formats_.find(format) != known_custom_formats_.end() || pickle_data_.find(format) != pickle_data_.end(); } +bool OSExchangeDataProviderGtk::GetPlainTextURL(GURL* url) const { + if ((formats_ & OSExchangeData::STRING) == 0) + return false; + + GURL test_url(string_); + if (!test_url.is_valid()) + return false; + + if (url) + *url = test_url; + return true; +} + /////////////////////////////////////////////////////////////////////////////// // OSExchangeData, public: diff --git a/app/os_exchange_data_provider_gtk.h b/app/os_exchange_data_provider_gtk.h index 5974cde..ffbb182 100644 --- a/app/os_exchange_data_provider_gtk.h +++ b/app/os_exchange_data_provider_gtk.h @@ -59,27 +59,23 @@ class OSExchangeDataProviderGtk : public OSExchangeData::Provider { virtual void SetFilename(const std::wstring& full_path); virtual void SetPickledData(OSExchangeData::CustomFormat format, const Pickle& data); - virtual void SetFileContents(const std::wstring& filename, - const std::string& file_contents); - virtual void SetHtml(const std::wstring& html, const GURL& base_url); virtual bool GetString(std::wstring* data) const; virtual bool GetURLAndTitle(GURL* url, std::wstring* title) const; virtual bool GetFilename(std::wstring* full_path) const; virtual bool GetPickledData(OSExchangeData::CustomFormat format, Pickle* data) const; - virtual bool GetFileContents(std::wstring* filename, - std::string* file_contents) const; - virtual bool GetHtml(std::wstring* html, GURL* base_url) const; virtual bool HasString() const; virtual bool HasURL() const; virtual bool HasFile() const; - virtual bool HasFileContents() const; - virtual bool HasHtml() const; virtual bool HasCustomFormat(OSExchangeData::CustomFormat format) const; private: typedef std::map<OSExchangeData::CustomFormat, Pickle> PickleData; + // Returns true if |formats_| contains a string format and the string can be + // parsed as a URL. + bool GetPlainTextURL(GURL* url) const; + // These are the possible formats the OSExchangeData may contain. Don't // confuse this with the actual formats that have been set, which are // |formats_| and |custom_formats_|. @@ -97,13 +93,8 @@ class OSExchangeDataProviderGtk : public OSExchangeData::Provider { GURL url_; string16 title_; - // File contents. - string16 filename_; - std::string file_contents_; - - // HTML contents. - string16 html_; - GURL base_url_; + // File name. + std::string filename_; // PICKLED_DATA contents. PickleData pickle_data_; |