Back to index

lightning-sunbird  0.9+nobinonly
nsMime.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Lars Knoll <knoll@kde.org>
00024  *   Zack Rusin <zack@kde.org>
00025  *   Denis Issoupov <denis@macadamian.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsDragService.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsXPCOM.h"
00044 #include "nsISupportsPrimitives.h"
00045 #include "nsXPIDLString.h"
00046 #include "nsPrimitiveHelpers.h"
00047 #include "nsReadableUtils.h"
00048 #include "nsClipboard.h"
00049 #include "nsMime.h"
00050 #include "nsCRT.h"
00051 
00052 #include <qapplication.h>
00053 #include <qclipboard.h>
00054 
00055 //----------------------------------------------------------
00056 nsMimeStoreData::nsMimeStoreData(const QCString &name, const QByteArray &data)
00057 {
00058     flavorName = name;
00059     flavorData = data;
00060 }
00061 
00062 nsMimeStoreData::nsMimeStoreData(const char *name, void *rawdata, PRInt32 rawlen)
00063 {
00064     flavorName = name;
00065     flavorData.assign((char*)rawdata,(unsigned int)rawlen);
00066 }
00067 
00068 //----------------------------------------------------------
00069 nsMimeStore::nsMimeStore()
00070 {
00071     mMimeStore.setAutoDelete(TRUE);
00072 }
00073 
00074 nsMimeStore::~nsMimeStore()
00075 {
00076 }
00077 
00078 const char* nsMimeStore::format(int n) const
00079 {
00080     if (n >= (int)mMimeStore.count())
00081         return 0;
00082 
00083     // because of const
00084 
00085     const nsMimeStoreData* msd;
00086     msd = mMimeStore.at(n);
00087 
00088     return msd->flavorName;
00089 }
00090 
00091 QByteArray nsMimeStore::encodedData(const char* name) const
00092 {
00093     QByteArray qba;
00094 
00095     QString mime(name);
00096 
00097     /* XXX: for some reason in here we're getting mimetype
00098      * with charset, eg "text/plain;charset=UTF-8" so here
00099      * we remove the charset
00100      */
00101     int n = mime.findRev(";");
00102     mime = mime.remove(n, mime.length() - n);
00103 
00104     // because of const
00105     nsMimeStoreData* msd;
00106     QPtrList<nsMimeStoreData>::const_iterator it = mMimeStore.begin();
00107     while ((msd = *it)) {
00108         if (mime.utf8() == msd->flavorName) {
00109             qba = msd->flavorData;
00110             return qba;
00111         }
00112         ++it;
00113     }
00114 #ifdef NS_DEBUG
00115     printf("nsMimeStore::encodedData requested unknown %s\n", name);
00116 #endif
00117     return qba;
00118 }
00119 
00120 PRBool nsMimeStore::ContainsFlavor(const char* name)
00121 {
00122     for (nsMimeStoreData *msd = mMimeStore.first(); msd; msd = mMimeStore.next()) {
00123         if (!strcmp(name, msd->flavorName))
00124             return PR_TRUE;
00125     }
00126     return PR_FALSE;
00127 }
00128 
00129 PRBool nsMimeStore::AddFlavorData(const char* name, const QByteArray &data)
00130 {
00131     if (ContainsFlavor(name))
00132         return PR_FALSE;
00133     mMimeStore.append(new nsMimeStoreData(name, data));
00134 
00135     // we're done unless we're given text/unicode,
00136     // and text/plain is not already advertised,
00137     if (strcmp(name, kUnicodeMime) || ContainsFlavor(kTextMime))
00138         return PR_TRUE;
00139     // in which case we also advertise text/plain
00140     // which we will convert on our own in nsDataObj::GetText().
00141 
00142     // let's text/plain be first for stupid programs
00143     // Ownership of |as| is transfered to mMimeStore
00144     mMimeStore.insert(0,new nsMimeStoreData(kTextMime,data.data(),data.count() + 1));
00145     return PR_TRUE;
00146 }
00147 
00148 //----------------------------------------------------------
00149 nsDragObject::nsDragObject(nsMimeStore* mimeStore,QWidget* dragSource,
00150                            const char* name)
00151     : QDragObject(dragSource, name)
00152 {
00153     if (!mimeStore)
00154         NS_ASSERTION(PR_TRUE, "Invalid  pointer.");
00155 
00156     mMimeStore = mimeStore;
00157 }
00158 
00159 nsDragObject::~nsDragObject()
00160 {
00161     delete mMimeStore;
00162 }
00163 
00164 const char* nsDragObject::format(int i) const
00165 {
00166     if (i >= (int)mMimeStore->count())
00167         return 0;
00168 
00169     const char* frm = mMimeStore->format(i);
00170 #ifdef NS_DEBUG
00171     printf("nsDragObject::format i=%i %s\n",i, frm);
00172 #endif
00173     return frm;
00174 }
00175 
00176 QByteArray nsDragObject::encodedData(const char* frm) const
00177 {
00178 #ifdef NS_DEBUG
00179     printf("nsDragObject::encodedData %s\n",frm);
00180 #endif
00181     return mMimeStore->encodedData(frm);
00182 }