Changeset 249 for trunk/gtk


Ignore:
Timestamp:
May 3, 2006, 10:09:11 AM (16 years ago)
Author:
joshe
Message:

Some minor code cleanups.
Handle things a little better when quitting.

Location:
trunk/gtk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/conf.c

    r248 r249  
    2626
    2727#include <sys/types.h>
    28 #include <assert.h>
    2928#include <ctype.h>
    3029#include <errno.h>
     
    342341const char *
    343342cf_getpref(const char *name) {
    344   assert(NULL != gl_prefs);
     343  g_assert(NULL != gl_prefs);
    345344
    346345  return g_tree_lookup(gl_prefs, name);
     
    349348void
    350349cf_setpref(const char *name, const char *value) {
    351   assert(NULL != gl_prefs);
     350  g_assert(NULL != gl_prefs);
    352351
    353352  g_tree_insert(gl_prefs, g_strdup(name), g_strdup(value));
  • trunk/gtk/dialogs.c

    r248 r249  
    2525*/
    2626
    27 #include <assert.h>
    2827#include <errno.h>
    2928#include <stdlib.h>
     
    174173
    175174#undef RN
    176   assert(rowcount == ii);
     175  g_assert(rowcount == ii);
    177176
    178177  gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(wind)->vbox), table);
     
    496495  INFOSEP(table, ii);
    497496
    498   assert(rowcount == ii);
     497  g_assert(rowcount == ii);
    499498
    500499  gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(wind)->vbox), table);
  • trunk/gtk/ipc.c

    r248 r249  
    2828#include <sys/socket.h>
    2929#include <sys/un.h>
    30 #include <assert.h>
    3130#include <errno.h>
    3231#include <fcntl.h>
  • trunk/gtk/main.c

    r248 r249  
    2626
    2727#include <sys/param.h>
    28 #include <assert.h>
    2928#include <errno.h>
    3029#include <signal.h>
     
    4847#include "util.h"
    4948
     49/* time in seconds to wait for torrents to stop when exiting */
    5050#define TRACKER_EXIT_TIMEOUT    5
     51
     52/* interval in milliseconds to update the torrent list display */
     53#define UPDATE_INTERVAL         500
     54
     55/* interval in milliseconds to check for stopped torrents and update display */
     56#define EXIT_CHECK_INTERVAL     500
    5157
    5258struct cbdata {
     
    7379void
    7480makewind(GtkWidget *wind, TrBackend *back, benc_val_t *state, GList *args);
     81void
     82quittransmission(struct cbdata *data);
    7583GtkWidget *
    7684makewind_toolbar(struct cbdata *data);
     
    8189gboolean
    8290exitcheck(gpointer gdata);
    83 void
    84 stoptransmission(struct cbdata *data);
    8591void
    8692setupdrag(GtkWidget *widget, struct cbdata *data);
     
    163169
    164170static sig_atomic_t global_sigcount = 0;
    165 static int global_lastsig = 0;
    166171
    167172int
     
    286291  data->back = back;
    287292  data->wind = GTK_WINDOW(wind);
    288   data->timer = -1;
     293  data->timer = 0;
    289294  /* filled in by makewind_list */
    290295  data->model = NULL;
     
    307312  gtk_box_pack_start(GTK_BOX(vbox), status, FALSE, FALSE, 0);
    308313
     314  gtk_container_set_focus_child(GTK_CONTAINER(vbox), scroll);
    309315  gtk_container_add(GTK_CONTAINER(wind), vbox);
    310316  gtk_window_set_title(data->wind, g_get_application_name());
     
    315321  addtorrents(data, state, args, NULL, NULL);
    316322
    317   data->timer = g_timeout_add(500, updatemodel, data);
     323  data->timer = g_timeout_add(UPDATE_INTERVAL, updatemodel, data);
    318324  updatemodel(data);
    319325
     
    333339
    334340  ipc_socket_setup(GTK_WINDOW(wind), addtorrents, data);
     341}
     342
     343void
     344quittransmission(struct cbdata *data) {
     345  g_object_unref(G_OBJECT(data->back));
     346  gtk_widget_destroy(GTK_WIDGET(data->wind));
     347  if(0 < data->timer)
     348    g_source_remove(data->timer);
     349  if(NULL != data->stupidpopuphack)
     350    gtk_widget_destroy(data->stupidpopuphack);
     351  g_free(data->buttons);
     352  g_free(data);
     353  gtk_main_quit();
    335354}
    336355
     
    389408  char *str;
    390409
    391   assert(MC_ROW_COUNT == ALEN(types));
     410  g_assert(MC_ROW_COUNT == ALEN(types));
    392411
    393412  store = gtk_list_store_newv(MC_ROW_COUNT, types);
     
    434453  struct cbdata *data = gdata;
    435454  struct exitdata *edata;
    436   tr_stat_t *st;
    437   GtkTreeIter iter;
    438   TrTorrent *tor;
    439   gboolean going;
    440 
    441   if(0 >= data->timer)
     455  unsigned int ii;
     456
     457  /* stop the update timer */
     458  if(0 < data->timer)
    442459    g_source_remove(data->timer);
    443   data->timer = -1;
    444 
    445   going = gtk_tree_model_get_iter_first(data->model, &iter);
    446   while(going) {
    447     gtk_tree_model_get(data->model, &iter, MC_TORRENT, &tor, -1);
    448     st = tr_torrent_stat(tor);
    449     if(TR_STATUS_ACTIVE & st->status) {
    450       tr_torrentStop(tr_torrent_handle(tor));
    451       going = gtk_tree_model_iter_next(data->model, &iter);
    452     } else {
    453       going = gtk_list_store_remove(GTK_LIST_STORE(data->model), &iter);
    454     }
    455     g_object_unref(G_OBJECT(tor));
    456   }
    457 
    458   /* XXX should disable widgets or something */
    459 
    460   /* try to wait until torrents stop before exiting */
     460  data->timer = 0;
     461
     462  /* try to stop all the torrents */
     463  tr_backend_stop_torrents(data->back);
     464
     465  /* set things up to wait for torrents to stop */
    461466  edata = g_new0(struct exitdata, 1);
    462467  edata->cbdata = data;
    463468  edata->started = time(NULL);
    464   edata->timer = g_timeout_add(500, exitcheck, edata);
     469  /* check if torrents are still running */
     470  if(exitcheck(edata)) {
     471    /* yes, start the exit timer and disable widgets */
     472    edata->timer = g_timeout_add(EXIT_CHECK_INTERVAL, exitcheck, edata);
     473    for(ii = 0; ii < ALEN(actionitems); ii++)
     474      gtk_widget_set_sensitive(data->buttons[ii], FALSE);
     475    gtk_widget_set_sensitive(GTK_WIDGET(data->view), FALSE);
     476  }
    465477
    466478  /* returning FALSE means to destroy the window */
     
    471483exitcheck(gpointer gdata) {
    472484  struct exitdata *data = gdata;
    473   tr_stat_t *st;
    474   GtkTreeIter iter;
    475   TrTorrent *tor;
    476   gboolean go;
    477 
    478   go = gtk_tree_model_get_iter_first(data->cbdata->model, &iter);
    479   while(go) {
    480     gtk_tree_model_get(data->cbdata->model, &iter, MC_TORRENT, &tor, -1);
    481     st = tr_torrent_stat(tor);
    482     if(!(TR_STATUS_PAUSE & st->status))
    483       go = gtk_tree_model_iter_next(data->cbdata->model, &iter);
    484     else {
    485       go = gtk_list_store_remove(GTK_LIST_STORE(data->cbdata->model), &iter);
    486     }
    487     g_object_unref(G_OBJECT(tor));
    488   }
    489485
    490486  /* keep going if we still have torrents and haven't hit the exit timeout */
    491   if(0 < tr_torrentCount(tr_backend_handle(data->cbdata->back)) &&
    492      time(NULL) - data->started < TRACKER_EXIT_TIMEOUT) {
    493     assert(gtk_tree_model_get_iter_first(data->cbdata->model, &iter));
     487  if(time(NULL) - data->started < TRACKER_EXIT_TIMEOUT &&
     488     !tr_backend_torrents_stopped(data->cbdata->back)) {
    494489    updatemodel(data->cbdata);
    495490    return TRUE;
     
    497492
    498493  /* exit otherwise */
    499 
    500   if(0 >= data->timer)
     494  if(0 < data->timer)
    501495    g_source_remove(data->timer);
    502   data->timer = -1;
    503 
    504   stoptransmission(data->cbdata);
    505 
    506   gtk_widget_destroy(GTK_WIDGET(data->cbdata->wind));
    507   if(NULL != data->cbdata->stupidpopuphack)
    508     gtk_widget_destroy(data->cbdata->stupidpopuphack);
    509   g_free(data->cbdata->buttons);
    510   g_free(data->cbdata);
     496  quittransmission(data->cbdata);
    511497  g_free(data);
    512   gtk_main_quit();
    513498
    514499  return FALSE;
    515 }
    516 
    517 void
    518 stoptransmission(struct cbdata *data) {
    519   GtkTreeIter iter;
    520   TrTorrent *tor;
    521   gboolean go;
    522 
    523   go = gtk_tree_model_get_iter_first(data->model, &iter);
    524   while(go) {
    525     gtk_tree_model_get(data->model, &iter, MC_TORRENT, &tor, -1);
    526     go = gtk_list_store_remove(GTK_LIST_STORE(data->model), &iter);
    527     g_object_unref(G_OBJECT(tor));
    528   }
    529   g_assert(0 == tr_torrentCount(tr_backend_handle(data->back)));
    530 
    531   g_object_unref(G_OBJECT(data->back));
    532500}
    533501
     
    696664  else {
    697665    top = g_strdup("");
    698     assert("XXX unknown status");
     666    g_assert_not_reached();
    699667  }
    700668
     
    763731
    764732  if(0 < global_sigcount) {
    765     stoptransmission(data);
    766     global_sigcount = SIGCOUNT_MAX;
    767     raise(global_lastsig);
     733    quittransmission(data);
     734    return FALSE;
    768735  }
    769736
     
    929896    if(actionitems[info.off].act == info.act)
    930897      break;
    931   assert(info.off < ALEN(actionitems));
     898  g_assert(info.off < ALEN(actionitems));
    932899
    933900  gtk_tree_selection_selected_foreach(sel, popupaction, &info);
    934901
    935902  for(ii = info.dead; NULL != ii; ii = ii->next) {
    936     assert(gtk_tree_row_reference_valid(ii->data));
     903    g_assert(gtk_tree_row_reference_valid(ii->data));
    937904    path = gtk_tree_row_reference_get_path(ii->data);
    938905    gtk_tree_selection_unselect_path(info.sel, path);
    939906    if(gtk_tree_model_get_iter(data->model, &iter, path))
    940907       gtk_list_store_remove(GTK_LIST_STORE(data->model), &iter);
    941     else
    942       assert(!"bad path");
     908    else {
     909      g_assert_not_reached();
     910    }
    943911    gtk_tree_path_free(path);
    944912    gtk_tree_row_reference_free(ii->data);
     
    11421110  struct sigaction sa;
    11431111
    1144   global_lastsig = sig;
    1145 
    11461112  if(SIGCOUNT_MAX <= ++global_sigcount) {
    11471113    bzero(&sa, sizeof(sa));
  • trunk/gtk/tr_backend.c

    r248 r249  
    136136  self->disposed = TRUE;
    137137
    138   fprintf(stderr, "back dispose %p\n", self);
    139 
    140138  if(NULL != self->torrents) {
    141139    for(ii = self->torrents; NULL != ii; ii = ii->next)
     
    153151  GObjectClass *parent = g_type_class_peek(g_type_parent(TR_BACKEND_TYPE));
    154152  TrBackend *self = (TrBackend *)obj;
    155 
    156   fprintf(stderr, "back finalize %p\n", self);
    157153
    158154  if(NULL != self->handle)
     
    237233  back->torrents = g_list_remove(back->torrents, tor);
    238234}
     235
     236void
     237tr_backend_stop_torrents(TrBackend *back) {
     238  GList *ii;
     239
     240  TR_IS_BACKEND(back);
     241
     242  for(ii = back->torrents; NULL != ii; ii = ii->next)
     243    if(TR_STATUS_ACTIVE & tr_torrent_stat(ii->data)->status)
     244      tr_torrentStop(tr_torrent_handle(ii->data));
     245}
     246
     247gboolean
     248tr_backend_torrents_stopped(TrBackend *back) {
     249  GList *ii;
     250
     251  TR_IS_BACKEND(back);
     252
     253  for(ii = back->torrents; NULL != ii; ii = ii->next)
     254    if(TR_STATUS_ACTIVE & tr_torrent_stat(ii->data)->status)
     255      return FALSE;
     256
     257  return TRUE;
     258}
  • trunk/gtk/tr_backend.h

    r248 r249  
    4949tr_backend_load_state(TrBackend *back, benc_val_t *state, GList **errors);
    5050
     51void
     52tr_backend_stop_torrents(TrBackend *back);
     53
     54gboolean
     55tr_backend_torrents_stopped(TrBackend *back);
     56
    5157#ifdef TR_WANT_BACKEND_PRIVATE
    5258void
  • trunk/gtk/tr_torrent.c

    r248 r249  
    3333static void
    3434tr_torrent_dispose(GObject *obj);
    35 static void
    36 tr_torrent_finalize(GObject *obj);
    3735static void
    3836tr_torrent_set_folder(TrTorrent *tor);
     
    7169  gobject_class->get_property = tr_torrent_get_property;
    7270  gobject_class->dispose = tr_torrent_dispose;
    73   gobject_class->finalize = tr_torrent_finalize;
    7471
    7572  pspec = g_param_spec_pointer("torrent-handle", "Torrent handle",
     
    179176  self->disposed = TRUE;
    180177
    181   fprintf(stderr, "tor dispose %p\n", self);
    182 
    183178  if(NULL != self->handle) {
    184179    if(!tr_torrent_paused(self))
     
    195190  /* Chain up to the parent class */
    196191  parent->dispose(obj);
    197 }
    198 
    199 static void
    200 tr_torrent_finalize(GObject *obj) {
    201   GObjectClass *parent = g_type_class_peek(g_type_parent(TR_TORRENT_TYPE));
    202   TrTorrent *self = (TrTorrent *)obj;
    203 
    204   fprintf(stderr, "tor finalize %p\n", self);
    205 
    206   /* Chain up to the parent class */
    207   parent->finalize(obj);
    208192}
    209193
Note: See TracChangeset for help on using the changeset viewer.