Changeset 91 for trunk/gtk


Ignore:
Timestamp:
Feb 6, 2006, 5:48:26 AM (16 years ago)
Author:
joshe
Message:

Implement drag-and-drop to allow files to be dropped into the main window.
Use locale-independent functions when parsing state file.

Location:
trunk/gtk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/conf.c

    r89 r91  
    374374
    375375  /* skip any leading whitespace */
    376   while(isspace((int)*line))
     376  while(g_ascii_isspace(*line))
    377377    line++;
    378378
    379379  /* walk over the key, which may be alphanumerics as well as - or _ */
    380   for(start = line; isalnum((int)*start) || '_' == *start || '-' == *start; start++)
     380  for(start = line; g_ascii_isalnum(*start)
     381        || '_' == *start || '-' == *start; start++)
    381382    ;
    382383
  • trunk/gtk/main.c

    r89 r91  
    8383void
    8484stoptransmission(void *tr);
     85void
     86setupdrag(GtkWidget *widget, struct cbdata *data);
     87void
     88gotdrag(GtkWidget *widget, GdkDragContext *dc, gint x, gint y,
     89        GtkSelectionData *sel, guint info, guint time, gpointer gdata);
    8590GtkWidget *
    8691makewind_toolbar(struct cbdata *data);
     
    295300  g_signal_connect(G_OBJECT(wind), "delete_event", G_CALLBACK(winclose), data);
    296301
     302  setupdrag(list, data);
     303
    297304  loaderrs = NULL;
    298305  for(ii = g_list_first(saved); NULL != ii; ii = ii->next) {
     
    428435    tr_torrentClose(tr, 0);
    429436  tr_close(tr);
     437}
     438
     439void
     440gotdrag(GtkWidget *widget SHUTUP, GdkDragContext *dc, gint x SHUTUP,
     441        gint y SHUTUP, GtkSelectionData *sel, guint info SHUTUP, guint time,
     442        gpointer gdata) {
     443  struct cbdata *data = gdata;
     444  char prefix[] = "file://";
     445  char *file, *sele, *targ, *type;
     446  int ii;
     447
     448  if(gdk_atom_intern("XdndSelection", FALSE) == sel->selection &&
     449     8 == sel->format && (int)sizeof(prefix) - 1 < sel->length &&
     450     0 == strncmp(prefix, sel->data, sizeof(prefix) - 1)) {
     451    file = urldecode(sel->data + (sizeof(prefix) - 1),
     452                     sel->length - (sizeof(prefix) - 1));
     453    if(g_utf8_validate(file, -1, NULL) &&
     454       addtorrent(data->tr, data->wind, file, NULL, FALSE, NULL)) {
     455      g_free(file);
     456      gtk_drag_finish(dc, TRUE, FALSE, time);
     457      addedtorrents(data);
     458      return;
     459    }
     460    g_free(file);
     461  } else {
     462    sele = gdk_atom_name(sel->selection);
     463    targ = gdk_atom_name(sel->target);
     464    type = gdk_atom_name(sel->type);
     465    fprintf(stderr, "unhandled drag: sel=%s targ=%s type=%s fmt=%i len=%i\n",
     466            sele, targ, type, sel->format, sel->length);
     467    g_free(sele);
     468    g_free(targ);
     469    g_free(type);
     470    if(8 == sel->format) {
     471      for(ii = 0; ii < sel->length; ii++)
     472        fprintf(stderr, "%02X ", sel->data[ii]);
     473      fprintf(stderr, "\n");
     474    }
     475  }
     476
     477  gtk_drag_finish(dc, FALSE, FALSE, time);
     478}
     479
     480void
     481setupdrag(GtkWidget *widget, struct cbdata *data) {
     482  GtkTargetEntry targets[] = {
     483    { "STRING",     0, 0 },
     484    { "text/plain", 0, 0 },
     485    { "text/uri-list", 0, 0 },
     486  };
     487
     488  g_signal_connect(widget, "drag_data_received", G_CALLBACK(gotdrag), data);
     489
     490  gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_ALL, targets,
     491                    ALEN(targets), GDK_ACTION_COPY);
    430492}
    431493
  • trunk/gtk/util.c

    r89 r91  
    129129}
    130130
     131char *
     132urldecode(const char *str, int len) {
     133  int ii, jj;
     134  char *ret;
     135  char buf[3];
     136
     137  if(0 >= len)
     138    len = strlen(str);
     139
     140  for(ii = jj = 0; ii < len; ii++, jj++)
     141    if('%' == str[ii])
     142      ii += 2;
     143
     144  ret = g_new(char, jj + 1);
     145
     146  buf[2] = '\0';
     147  for(ii = jj = 0; ii < len; ii++, jj++) {
     148    switch(str[ii]) {
     149      case '%':
     150        if(ii + 2 < len) {
     151          buf[0] = str[ii+1];
     152          buf[1] = str[ii+2];
     153          ret[jj] = g_ascii_strtoull(buf, NULL, 16);
     154        }
     155        ii += 2;
     156        break;
     157      case '+':
     158        ret[jj] = ' ';
     159      default:
     160        ret[jj] = str[ii];
     161    }
     162  }
     163  ret[jj] = '\0';
     164
     165  return ret;
     166}
     167
    131168static int exit_sigs[] = {SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2};
    132169static callbackfunc_t exit_func = NULL;
  • trunk/gtk/util.h

    r89 r91  
    6262joinstrlist(GList *list, char *sep);
    6363
     64/* decodes a string that has been urlencoded */
     65char *
     66urldecode(const char *str, int len);
     67
    6468/* set up a handler for various fatal signals */
    6569void
Note: See TracChangeset for help on using the changeset viewer.