diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..3dd9704 --- /dev/null +++ b/src/main.c @@ -0,0 +1,166 @@ +#include "common.h" + +#include <gtk/gtk.h> +#include "customcellrendererstate.h" +#include "customcellrendererprogress.h" +#include "customcellrendererrate.h" +#include "customcellrendererleft.h" +#include <string.h> + +typedef enum +{ + COLUMN_STATE, + COLUMN_TITLE, + COLUMN_DOWNLOADED, + COLUMN_SEEDED, + COLUMN_UP, + COLUMN_DOWN, + COLUMN_LEFT, + COLUMN_PROGRESSSORT, +} column_t; + +typedef struct +{ + state_t state; + gchar* title; + gfloat downloaded; /* percent */ + gfloat seeded; /* ratio */ + gfloat down, up; /* kilo (1000) byte per second */ + guint64 size; /* bytes */ + gint64 left; /* seconds left until done, < 0 means unknown */ +} torrent_t; + +static void torrent_update(torrent_t* torrent, + GtkListStore* store, GtkTreeIter* iter); + +int main(int argc, char** argv) +{ + const gchar* gladefile; + GtkBuilder* builder; + GError* error = NULL; + guint ret; + GtkWidget* top; + GtkWidget* listview; + GtkListStore* liststore; + GtkTreeIter iter; + GtkCellRenderer* cell; + GtkTreeViewColumn* column; + + g_thread_init(NULL); + + gtk_init(&argc, &argv); + + builder = gtk_builder_new(); + gladefile = DATAROOTDIR "viewtorrents/viewtorrents.glade"; + ret = gtk_builder_add_from_file(builder, gladefile, &error); +#ifdef DEBUG + if (ret == 0) + { + g_clear_error(&error); + gladefile = "gui/viewtorrents.glade"; + ret = gtk_builder_add_from_file(builder, gladefile, &error); + } +#endif + if (ret == 0) + { + fprintf(stderr, "Unable to load %s: %s\n", gladefile, error->message); + g_clear_error(&error); + return EXIT_FAILURE; + } + + top = GTK_WIDGET(gtk_builder_get_object(builder, "main")); + + g_signal_connect(top, "delete-event", G_CALLBACK(gtk_main_quit), NULL); + g_signal_connect(gtk_builder_get_object(builder, "quitmenuitem"), + "activate", G_CALLBACK(gtk_main_quit), NULL); + + listview = GTK_WIDGET(gtk_builder_get_object(builder, "treeview")); + liststore = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststore")); + + cell = custom_cell_renderer_state_new(); + column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "statecolumn")); + gtk_tree_view_column_pack_start(column, cell, TRUE); + gtk_tree_view_column_add_attribute(column, cell, "state", COLUMN_STATE); + + cell = custom_cell_renderer_progress_new(); + column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "progresscolumn")); + gtk_tree_view_column_pack_start(column, cell, TRUE); + gtk_tree_view_column_add_attribute(column, cell, "downloaded", COLUMN_DOWNLOADED); + gtk_tree_view_column_add_attribute(column, cell, "seeded", COLUMN_SEEDED); + + cell = custom_cell_renderer_rate_new(); + column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "upcolumn")); + gtk_tree_view_column_pack_start(column, cell, TRUE); + gtk_tree_view_column_add_attribute(column, cell, "rate", COLUMN_UP); + + column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "downcolumn")); + gtk_tree_view_column_pack_start(column, cell, TRUE); + gtk_tree_view_column_add_attribute(column, cell, "rate", COLUMN_DOWN); + + cell = custom_cell_renderer_left_new(); + column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "leftcolumn")); + gtk_tree_view_column_pack_start(column, cell, TRUE); + gtk_tree_view_column_add_attribute(column, cell, "left", COLUMN_LEFT); + + { + torrent_t torrent; + torrent.state = STATE_DEAD; + torrent.title = "The.Tree.of.Life.2011.720p.BluRay.x264.DTS-HDxT"; + torrent.seeded = 3.0; + torrent.downloaded = 100.0; + torrent.up = 0.0; + torrent.down = 0.0; + torrent.left = -1; + + gtk_list_store_append(liststore, &iter); + torrent_update(&torrent, liststore, &iter); + } + { + torrent_t torrent; + torrent.state = STATE_ACTIVE; + torrent.title = "Burn.Notice.S05E14.720p.HDTV.x264-AVS.mkv"; + torrent.seeded = 2.79; + torrent.downloaded = 100.0; + torrent.up = 72.5; + torrent.down = 0.0; + torrent.left = -1; + + gtk_list_store_append(liststore, &iter); + torrent_update(&torrent, liststore, &iter); + } + { + torrent_t torrent; + torrent.state = STATE_ACTIVE; + torrent.title = "CSI.S12E07.720p.HDTV.X264-DIMENSION.mkv"; + torrent.seeded = 1.5; + torrent.downloaded = 73.45; + torrent.up = 37.3; + torrent.down = 994.8; + torrent.left = 24340; + + gtk_list_store_append(liststore, &iter); + torrent_update(&torrent, liststore, &iter); + } + + gtk_widget_show_all(top); + + gtk_main(); + + return EXIT_SUCCESS; +} + +void torrent_update(torrent_t* torrent, GtkListStore* store, GtkTreeIter* iter) +{ + gtk_list_store_set(store, iter, + COLUMN_STATE, torrent->state, + COLUMN_TITLE, torrent->title, + COLUMN_DOWNLOADED, torrent->downloaded, + COLUMN_SEEDED, torrent->seeded, + COLUMN_UP, torrent->up, + COLUMN_DOWN, torrent->down, + COLUMN_LEFT, torrent->left, + COLUMN_PROGRESSSORT, + torrent->downloaded < 100.0f ? torrent->downloaded : + torrent->seeded + 1000.0f, + -1); +} |
