Back to index

lightning-sunbird  0.9+nobinonly
Classes | Typedefs | Functions | Variables
nsClipboard.cpp File Reference
#include "nsClipboard.h"
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include "nsCOMPtr.h"
#include "nsCRT.h"
#include "nsISupportsArray.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsReadableUtils.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsWidgetsCID.h"
#include "nsXPIDLString.h"
#include "nsUnicharUtils.h"
#include "nsPrimitiveHelpers.h"
#include "nsTextFormatter.h"
#include "prtime.h"
#include "prthread.h"
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "nsIPlatformCharset.h"
#include "nsICharsetConverterManager.h"

Go to the source code of this file.

Classes

struct  _GtkSelectionTargetList
struct  checkEventContext

Typedefs

typedef struct _GtkSelectionTargetList

Functions

static void ConvertHTMLtoUCS2 (char *data, PRInt32 dataLength, PRUnichar **unicodeData, PRInt32 &outUnicodeLen)
static void GetHTMLCharset (char *data, PRInt32 dataLength, nsACString &str)
void __gtk_selection_target_list_remove (GtkWidget *widget, GdkAtom selection)
static void DispatchSelectionNotifyEvent (GtkWidget *widget, XEvent *xevent)
static void DispatchPropertyNotifyEvent (GtkWidget *widget, XEvent *xevent)
static Bool checkEventProc (Display *display, XEvent *event, XPointer arg)

Variables

static GdkAtom GDK_SELECTION_CLIPBOARD
static const char * gtk_selection_handler_key = "gtk-selection-handlers"
static const int kClipboardTimeout = 500000

Class Documentation

struct _GtkSelectionTargetList

Definition at line 127 of file nsClipboard.cpp.

Class Members
GtkTargetList * list
GdkAtom selection
struct checkEventContext

Definition at line 1330 of file nsClipboard.cpp.

Class Members
GtkWidget * cbWidget
Atom selAtom

Typedef Documentation

typedef struct _GtkSelectionTargetList

Definition at line 125 of file nsClipboard.cpp.


Function Documentation

void __gtk_selection_target_list_remove ( GtkWidget *  widget,
GdkAtom  selection 
)

Definition at line 134 of file nsClipboard.cpp.

{
  GtkSelectionTargetList *sellist;
  GList *tmp_list, *tmp_list2;
  GList *lists;
  lists = (GList*)gtk_object_get_data (GTK_OBJECT (widget), gtk_selection_handler_key);
  tmp_list = lists;
  while (tmp_list) {
    sellist = (GtkSelectionTargetList*)tmp_list->data;
    if (sellist->selection == selection) {
      gtk_target_list_unref (sellist->list);
      g_free (sellist);
      tmp_list->data = nsnull;
      tmp_list2 = tmp_list->prev;
      lists = g_list_remove_link(lists, tmp_list);
      g_list_free_1(tmp_list);
      tmp_list = tmp_list2;
    }
    if (tmp_list)
      tmp_list = tmp_list->next;
  }
  gtk_object_set_data(GTK_OBJECT(widget), gtk_selection_handler_key, lists);
}

Here is the call graph for this function:

static Bool checkEventProc ( Display display,
XEvent *  event,
XPointer  arg 
) [static]

Definition at line 1337 of file nsClipboard.cpp.

{
  checkEventContext *context = (checkEventContext *) arg;

  if (event->xany.type == SelectionNotify ||
      (event->xany.type == PropertyNotify &&
       event->xproperty.atom == context->selAtom)) {

    GdkWindow *cbWindow = gdk_window_lookup(event->xany.window);
    if (cbWindow) {
      GtkWidget *cbWidget = nsnull;
      gdk_window_get_user_data(cbWindow, (gpointer *)&cbWidget);
      if (cbWidget && GTK_IS_WIDGET(cbWidget)) {
        context->cbWidget = cbWidget;
        return True;
      }
    }
  }

  return False;
}

Here is the caller graph for this function:

void ConvertHTMLtoUCS2 ( char *  data,
PRInt32  dataLength,
PRUnichar **  unicodeData,
PRInt32 outUnicodeLen 
) [static]

Definition at line 1437 of file nsClipboard.cpp.

{
  nsCAutoString charset;
  GetHTMLCharset(data, dataLength, charset);// get charset of HTML
  if (charset.EqualsLiteral("UTF-16")) {//current mozilla
    outUnicodeLen = dataLength / 2 - 1;
    *unicodeData = NS_REINTERPRET_CAST(PRUnichar*,
                   nsMemory::Alloc((outUnicodeLen + 1) * sizeof(PRUnichar)));
    if ( unicodeData ) {
      memcpy(*unicodeData, data + sizeof(PRUnichar),
             outUnicodeLen * sizeof(PRUnichar));
      (*unicodeData)[outUnicodeLen] = '\0';
    }
  }
  else if (charset.EqualsLiteral("OLD-MOZILLA")) {// old mozilla
    outUnicodeLen = dataLength / 2;
    *unicodeData = NS_REINTERPRET_CAST(PRUnichar*,
                   nsMemory::Alloc((outUnicodeLen + 1) * sizeof(PRUnichar)));
    if ( unicodeData ) {
      memcpy(*unicodeData, data, outUnicodeLen * sizeof(PRUnichar));
      (*unicodeData)[outUnicodeLen] = '\0';
    }
  }
  else {// app which use "text/html" to copy&paste
    nsCOMPtr<nsIUnicodeDecoder> decoder;
    nsresult rv;
    // get the decoder
    nsCOMPtr<nsICharsetConverterManager> ccm =
            do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
    if (NS_FAILED(rv)) {
#ifdef DEBUG_CLIPBOARD
      g_print("        can't get CHARSET CONVERTER MANAGER service\n");
#endif
      outUnicodeLen = 0;
      return;
    }
    rv = ccm->GetUnicodeDecoder(charset.get(), getter_AddRefs(decoder));
    if (NS_FAILED(rv)) {
#ifdef DEBUG_CLIPBOARD
      g_print("        get unicode decoder error\n");
#endif
      outUnicodeLen = 0;
      return;
    }
    // converting
    decoder->GetMaxLength(data, dataLength, &outUnicodeLen);
    // |outUnicodeLen| is number of chars
    if (outUnicodeLen) {
      *unicodeData = NS_REINTERPRET_CAST(PRUnichar*,
                     nsMemory::Alloc((outUnicodeLen + 1) * sizeof(PRUnichar)));
      if ( unicodeData ) {
        PRInt32 numberTmp = dataLength;
        decoder->Convert(data, &numberTmp, *unicodeData, &outUnicodeLen);
#ifdef DEBUG_CLIPBOARD
        if (numberTmp != dataLength)
          printf("didn't consume all the bytes\n");
#endif
        // null terminate. Convert() doesn't do it for us
        (*unicodeData)[outUnicodeLen] = '\0';
      }
    } // if valid length
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void DispatchPropertyNotifyEvent ( GtkWidget *  widget,
XEvent *  xevent 
) [static]

Definition at line 1316 of file nsClipboard.cpp.

{
  if (gdk_window_get_events(widget->window) & GDK_PROPERTY_CHANGE_MASK) {
    GdkEvent event;
    event.property.type = GDK_PROPERTY_NOTIFY;
    event.property.window = widget->window;
    event.property.atom = xevent->xproperty.atom;
    event.property.time = xevent->xproperty.time;
    event.property.state = xevent->xproperty.state;

    gtk_widget_event(widget, &event);
  }
}

Here is the caller graph for this function:

static void DispatchSelectionNotifyEvent ( GtkWidget *  widget,
XEvent *  xevent 
) [static]

Definition at line 1302 of file nsClipboard.cpp.

{
  GdkEvent event;
  event.selection.type = GDK_SELECTION_NOTIFY;
  event.selection.window = widget->window;
  event.selection.selection = xevent->xselection.selection;
  event.selection.target = xevent->xselection.target;
  event.selection.property = xevent->xselection.property;
  event.selection.time = xevent->xselection.time;

  gtk_widget_event(widget, &event);
}

Here is the caller graph for this function:

void GetHTMLCharset ( char *  data,
PRInt32  dataLength,
nsACString &  str 
) [static]

Definition at line 1509 of file nsClipboard.cpp.

{
  // if detect "FFFE" or "FEFF", assume utf-16
  PRUnichar* beginChar =  (PRUnichar*)data;
  if ((beginChar[0] == 0xFFFE) || (beginChar[0] == 0xFEFF)) {
    str.AssignLiteral("UTF-16");
    return;
  }
  // no "FFFE" and "FEFF", assume ASCII first to find "charset" info
  nsDependentCString htmlStr = nsDependentCString(data, dataLength);
  nsACString::const_iterator start, end, valueStart, valueEnd;

  htmlStr.BeginReading(start);
  htmlStr.EndReading(end);
  htmlStr.BeginReading(valueStart);
  htmlStr.BeginReading(valueEnd);
  
  if (CaseInsensitiveFindInReadable(NS_LITERAL_CSTRING("CONTENT=\"text/html;"),
                                    start, end)) {
    start = end;
    htmlStr.EndReading(end);

    if (CaseInsensitiveFindInReadable(NS_LITERAL_CSTRING("charset="),
                                      start, end)) {
      valueStart = end;
      start = end;
      htmlStr.EndReading(end);
          
      if (CaseInsensitiveFindInReadable(NS_LITERAL_CSTRING("\""), start, end))
        valueEnd = start;
    }
  }
  // find "charset" in HTML
  if (valueStart != valueEnd) {
    const nsACString & charsetStr = Substring(valueStart, valueEnd);
    if ( !charsetStr.IsEmpty() ) {
      nsCString charsetUpperStr;
      ToUpperCase(charsetStr, charsetUpperStr);
#ifdef DEBUG_CLIPBOARD
      printf("Charset of HTML = %s\n", charsetUpperStr.get());
#endif
      str.Assign(charsetUpperStr);
      return;
    }
  }
  // no "charset" info, assume data come from old-version mozilla
  // [UCS2 encoding (without "FFFE" or "FEFF" at the beginning)]
  //
  // TODO: it may also be "text/html" without "charset".
  // can't distinguish between them. Sochoose OLD-MOZILLA here to
  // make compitable with old-version mozilla
  str.AssignLiteral("OLD-MOZILLA");
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

GdkAtom GDK_SELECTION_CLIPBOARD [static]

Definition at line 94 of file nsClipboard.cpp.

const char* gtk_selection_handler_key = "gtk-selection-handlers" [static]

Definition at line 132 of file nsClipboard.cpp.

const int kClipboardTimeout = 500000 [static]

Definition at line 1360 of file nsClipboard.cpp.