gnunet-developers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-developers] human size patch


From: Milan
Subject: [GNUnet-developers] human size patch
Date: Wed, 03 May 2006 12:02:03 +0200
User-agent: Mozilla Thunderbird 1.0.7 (X11/20051017)

That's done too. I now use asprintf from glibc, and I put the getHumanSize function in string.c in gnunet-util lib. If you think there's a better file to put it in, please feel free to do it.

In order to sort files by real size, I need to pass a callback function to free an integer (tricky issues from GTK+). I used the free() function instead of the FREE() macro because it wasn't working with the macro. Is this a problem ? Is there a workaround. Indeed, this is really insignificant, because I free 3 int so 6 bytes when the GtkTreeView are closed, and they would have been freed automatically when gnunet-gtk stops. But let's be clean...

I saw that gnunet-gtk has been improved recently (libnotify, resizing columns and ordering, downloading from URI, showing the extracted keywords before insertion...). This is just great !
diff -r GNUnet/src/include/gnunet_util.h GNUnet-edit/src/include/gnunet_util.h
2506a2507,2512
>  * @brief Get human-readable filesizes from byte numbers
>  * @param size_n the size in bytes
>  */
> char * getHumanSize (unsigned long long int size_n);
> 
> /**
diff -r GNUnet/src/util/string.c GNUnet-edit/src/util/string.c
91c91,122
< #endif
---
> #endif
> 
> /*
> * Little method to get human-readable filesizes from byte numbers.
> */
> 
> #define KIBIBYTE_SIZE 1024
> #define MEBIBYTE_SIZE 1048576
> #define GIBIBYTE_SIZE 1073741824
> 
> char * getHumanSize (unsigned long long int size_n)
> {
>   long double size_d;
>   char * size = NULL;
> 
>   if (size_n == 0) {
>     size = STRDUP(_("unknown")); }
>   else if (size_n > GIBIBYTE_SIZE) {
>     size_d = (long double) size_n / GIBIBYTE_SIZE;
>     asprintf (&size, "%.1Lf %s", size_d, _("GiB")); }
>   else if (size_n > MEBIBYTE_SIZE) {
>     size_d = (long double) size_n / MEBIBYTE_SIZE;
>     asprintf (&size, "%.1Lf %s", size_d, _("MiB")); }
>   else if (size_n > KIBIBYTE_SIZE) {
>     size_d = (long double) size_n / KIBIBYTE_SIZE;
>     asprintf (&size, "%.1Lf %s", size_d, _("KiB")); }
>   else {
>     size_d = (long double) size_n;
>     asprintf (&size, "%.1Lf %s", size_d, _("Bytes")); }
> 
>   return size;
> }
diff -r gnunet-gtk/src/plugins/fs/download.c 
gnunet-gtk-edit/src/plugins/fs/download.c
116a117,118
>   char * size_h;
>   unsigned long long int size;
258d259
< 
299a301,305
> 
>   /* Set filesize (exact & human-readable) */
>   size = ECRS_fileSize(uri);
>   size_h = getHumanSize(size);
> 
308c314,315
<                      DOWNLOAD_SIZE, ECRS_fileSize(uri),
---
>                      DOWNLOAD_SIZE, size,
>                    DOWNLOAD_HSIZE, size_h,
316a324
>   FREE(size_h);
362a371,372
>   unsigned long long int size;
>   char * size_h;
404a415,419
> 
>   /* set filesize (exact & human-readable) */
>   size = ECRS_fileSize(uri);
>   size_h = getHumanSize (size);
> 
413c428,429
<                      DOWNLOAD_SIZE, ECRS_fileSize(uri),
---
>                    DOWNLOAD_SIZE, size,
>                      DOWNLOAD_HSIZE, size_h,
436a453
>   FREE(size_h);
659a677
>   char * filesize_h;
665a684
>   filesize_h = getHumanSize (filesize);
680a700
>                      DOWNLOAD_HSIZE, filesize_h,
686a707
>   FREE (filesize_h);
696a718
>   int * size_col_number;
705c727,728
<                        G_TYPE_UINT64,  /* size */
---
>                      G_TYPE_UINT64, /* size (exact, in bytes) */
>                        G_TYPE_STRING,  /* size (user-friendly) */
731a755
>   g_object_set (renderer, "xalign", 1.00);
736c760
<                                               "text", DOWNLOAD_SIZE,
---
>                                               "text", DOWNLOAD_HSIZE,
741a766,772
>   size_col_number = malloc(sizeof(int));
>   *size_col_number = DOWNLOAD_SIZE;
>   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(summary),
>                                              DOWNLOAD_HSIZE,
>                                              sortEntriesBySize,
>                                              size_col_number,
>                                              (void *) free);
743c774
<   gtk_tree_view_column_set_sort_column_id(column, DOWNLOAD_SIZE);
---
>   gtk_tree_view_column_set_sort_column_id(column, DOWNLOAD_HSIZE);
diff -r gnunet-gtk/src/plugins/fs/fs.c gnunet-gtk-edit/src/plugins/fs/fs.c
157a158,179
> 
> /* Sort entries in a TreeView using exact size (GTK callback function) */
> int sortEntriesBySize (GtkTreeModel *model,
>                        GtkTreeIter *a,
>                        GtkTreeIter *b,
>                        void * user_data)
> {
>   unsigned long long int size_a = 0;
>   unsigned long long int size_b = 0;
>   int * column_number = user_data;
> 
>   gtk_tree_model_get(model, a, *column_number, &size_a, -1);
>   gtk_tree_model_get(model, b, *column_number, &size_b, -1);
> 
>   if (size_a > size_b)
>     return 1;
>   else if (size_a < size_b)
>     return -1;
>   else
>     return 0;
> }
> 
diff -r gnunet-gtk/src/plugins/fs/fs.h gnunet-gtk-edit/src/plugins/fs/fs.h
35a36
>   SEARCH_HSIZE,
63a65
>   DOWNLOAD_HSIZE,
74a77
>   NAMESPACE_HSIZE,
83a87
>   IN_NAMESPACE_HSIZE,
117a122,131
> /* @brief Sort entries in a TreeView using exact size (GTK callback function)
>    @param model the model to sort 
>    @param a the first iterator
>    @param b the second iterator
>    @param user_data a pointer to an int giving the column number of exact 
> size */
> int sortEntriesBySize (GtkTreeModel *model,
>                        GtkTreeIter *a,
>                        GtkTreeIter *b,
>                        void * user_data);
> 
diff -r gnunet-gtk/src/plugins/fs/namespace.c 
gnunet-gtk-edit/src/plugins/fs/namespace.c
64a65
>   int *size_col_number;
83c84,85
<                      G_TYPE_UINT64,  /* size */
---
>                      G_TYPE_UINT64,  /* size (exact, in bytes) */
>                        G_TYPE_STRING, /* size (user-friendly) */
112a115
>   g_object_set (renderer, "xalign", 1.00);
117c120
<                                             "text", IN_NAMESPACE_SIZE,
---
>                                             "text", IN_NAMESPACE_HSIZE,
122a126,132
>   size_col_number = malloc(sizeof(int));
>   *size_col_number = IN_NAMESPACE_SIZE;
>   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(model),
>                                    IN_NAMESPACE_HSIZE,
>                                    sortEntriesBySize,
>                                    size_col_number,
>                                    (void *) free);
124c134
<   gtk_tree_view_column_set_sort_column_id(column, IN_NAMESPACE_SIZE);
---
>   gtk_tree_view_column_set_sort_column_id(column, IN_NAMESPACE_HSIZE);
250c260,261
<   unsigned long long size;
---
>   char * size_h;
>   unsigned long long int size;
288c299
<   if (ECRS_isFileUri(fi->uri))
---
>   if (ECRS_isFileUri(fi->uri)) {
290c301,302
<   else
---
>     size_h = getHumanSize(size); }
>   else {
291a304
>     size_h = STRDUP(_("unknown")); }
298a312
>                      NAMESPACE_HSIZE, size_h,
304a319
>   FREE(size_h);
391a407
>   char * size_h;
429c445
<   if (ECRS_isFileUri(fi->uri))
---
>   if (ECRS_isFileUri(fi->uri)) {
431c447,448
<   else
---
>     size_h = getHumanSize(size); }
>   else {
432a450
>     size_h = STRDUP(_("unknown")); }
452a471
>                      IN_NAMESPACE_HSIZE, size_h,
466a486
>   FREE(size_h);
1184a1205
>   int * size_col_number;
1199c1220,1221
<                            G_TYPE_UINT64, /* size */
---
>                              G_TYPE_UINT64, /* size (exact, in bytes) */
>                            G_TYPE_STRING, /* size (user-friendly) */
1208c1230
<                                             _("Filename"),
---
>                                             _("Name"),
1223a1246
>   g_object_set (renderer, "xalign", 1.00);
1226c1249
<                                             _("Filesize"),
---
>                                             _("Size"),
1228c1251
<                                             "text", NAMESPACE_SIZE,
---
>                                             "text", NAMESPACE_HSIZE,
1233a1257,1263
>   size_col_number = malloc(sizeof(int));
>   *size_col_number = NAMESPACE_SIZE;
>   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(model),
>                                    NAMESPACE_HSIZE,
>                                    sortEntriesBySize,
>                                    size_col_number,
>                                    (void *) free);
1235c1265
<   gtk_tree_view_column_set_sort_column_id(column, NAMESPACE_SIZE);
---
>   gtk_tree_view_column_set_sort_column_id(column, NAMESPACE_HSIZE);
diff -r gnunet-gtk/src/plugins/fs/search.c 
gnunet-gtk-edit/src/plugins/fs/search.c
67a68,69
>   char * size_h;
>   unsigned long long int size;
72d73
<   unsigned long long size;
112,113c113,114
<     
<   if (ECRS_isFileUri(uri)) {
---
> 
>    if (ECRS_isFileUri(uri)) {
114a116
>     size_h = getHumanSize(size);
117c119,121
<   }
---
>     size_h = STRDUP(_("unknown"));
>   } 
> 
139a144
>                      SEARCH_HSIZE, size_h,
149a155
>   FREE(size_h);
590a597
>   int * size_col_number;
609c616,617
<                      G_TYPE_UINT64,  /* size */
---
>                        G_TYPE_UINT64, /* size (exact, in bytes) */
>                      G_TYPE_STRING,  /* size (user-friendly) */
636a645
>   g_object_set (renderer, "xalign", 1.00);
641c650
<                                             "text", SEARCH_SIZE,
---
>                                             "text", SEARCH_HSIZE,
646a656,662
>   size_col_number = malloc(sizeof(int));
>   *size_col_number = SEARCH_SIZE;
>   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(tree),
>                                              SEARCH_HSIZE,
>                                              sortEntriesBySize,
>                                              size_col_number,
>                                              (void *) free);
648c664,665
<   gtk_tree_view_column_set_sort_column_id(column, SEARCH_SIZE);
---
>   gtk_tree_view_column_set_sort_column_id(column, SEARCH_HSIZE);
> 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]