diff options
Diffstat (limited to 'hardinfo/util.c')
-rw-r--r-- | hardinfo/util.c | 219 |
1 files changed, 98 insertions, 121 deletions
diff --git a/hardinfo/util.c b/hardinfo/util.c index dd5af1a4..6ddf56ad 100644 --- a/hardinfo/util.c +++ b/hardinfo/util.c @@ -91,40 +91,49 @@ gchar *find_program(gchar *program_name) gchar *seconds_to_string(unsigned int seconds) { unsigned int hours, minutes, days; + const gchar *days_fmt, *hours_fmt, *minutes_fmt, *seconds_fmt; + gchar *full_fmt, *ret = g_strdup(""); minutes = seconds / 60; + seconds %= 60; hours = minutes / 60; minutes %= 60; days = hours / 24; hours %= 24; - gchar *wminutes; - gchar *whours; - gchar *wdays; - - wdays = ngettext("%d day, ", "%d days, ", days); - whours = ngettext("%d hour, ", "%d hours, ", hours); - wminutes = ngettext("%d minute", "%d minutes", minutes); - if (days < 1) { - if (hours < 1) - return g_strdup_printf(ngettext("%d minute", "%d minutes", minutes), minutes); - return g_strdup_printf(whours, wminutes); + days_fmt = ngettext("%d day", "%d days", days); + hours_fmt = ngettext("%d hour", "%d hours", hours); + minutes_fmt = ngettext("%d minute", "%d minutes", minutes); + seconds_fmt = ngettext("%d second", "%d seconds", seconds); + + if (days) { + full_fmt = g_strdup_printf("%s %s %s %s", days_fmt, hours_fmt, minutes_fmt, seconds_fmt); + ret = g_strdup_printf(full_fmt, days, hours, minutes, seconds); + } else if (hours) { + full_fmt = g_strdup_printf("%s %s %s", hours_fmt, minutes_fmt, seconds_fmt); + ret = g_strdup_printf(full_fmt, hours, minutes, seconds); + } else if (minutes) { + full_fmt = g_strdup_printf("%s %s", minutes_fmt, seconds_fmt); + ret = g_strdup_printf(full_fmt, minutes, seconds); + } else { + ret = g_strdup_printf(seconds_fmt, seconds); } - return g_strdup_printf(wdays, whours, wminutes); + g_free(full_fmt); + return ret; } gchar *size_human_readable(gfloat size) { if (size < KiB) - return g_strdup_printf(_("%.1f B"), size); + return g_strdup_printf(_("%.1f B"), size); if (size < MiB) - return g_strdup_printf(_("%.1f KiB"), size / KiB); + return g_strdup_printf(_("%.1f KiB"), size / KiB); if (size < GiB) - return g_strdup_printf(_("%.1f MiB"), size / MiB); + return g_strdup_printf(_("%.1f MiB"), size / MiB); if (size < TiB) - return g_strdup_printf(_("%.1f GiB"), size / GiB); + return g_strdup_printf(_("%.1f GiB"), size / GiB); if (size < PiB) - return g_strdup_printf(_("%.1f TiB"), size / TiB); + return g_strdup_printf(_("%.1f TiB"), size / TiB); return g_strdup_printf(_("%.1f PiB"), size / PiB); } @@ -160,15 +169,24 @@ void remove_linefeed(gchar * str) void widget_set_cursor(GtkWidget * widget, GdkCursorType cursor_type) { GdkCursor *cursor; - - if ((cursor = gdk_cursor_new(cursor_type))) { - gdk_window_set_cursor(GDK_WINDOW(widget->window), cursor); - gdk_display_flush(gtk_widget_get_display(widget)); + GdkDisplay *display; + GdkWindow *gdk_window; + + display = gtk_widget_get_display(widget); + cursor = gdk_cursor_new_for_display(display, cursor_type); + gdk_window = gtk_widget_get_window(widget); + if (cursor) { + gdk_window_set_cursor(gdk_window, cursor); + gdk_display_flush(display); +#if GTK_CHECK_VERSION(3, 0, 0) + g_object_unref(cursor); +#else gdk_cursor_unref(cursor); +#endif } while (gtk_events_pending()) - gtk_main_iteration(); + gtk_main_iteration(); } static gboolean __nonblock_cb(gpointer data) @@ -721,6 +739,8 @@ static ShellModule *module_load(gchar * filename) entries = get_module_entries(); while (entries[i].name) { + if (*entries[i].name == '#') { i++; continue; } /* skip */ + ShellModuleEntry *entry = g_new0(ShellModuleEntry, 1); if (params.gui_running) { @@ -966,105 +986,6 @@ gint tree_view_get_visible_height(GtkTreeView * tv) return nrows * rect.height; } -void tree_view_save_image(gchar * filename) -{ - /* this is ridiculously complicated :/ why in the hell gtk+ makes this kind of - thing so difficult? */ - - /* FIXME: this does not work if the window (or part of it) isn't visible. does - anyone know how to fix this? :/ */ - Shell *shell = shell_get_main_shell(); - GtkWidget *widget = shell->info->view; - - PangoLayout *layout; - PangoContext *context; - PangoRectangle rect; - - GdkPixmap *pm; - GdkPixbuf *pb; - GdkGC *gc; - GdkColor black = { 0, 0, 0, 0 }; - GdkColor white = { 0, 65535, 65535, 65535 }; - - gint w, h, visible_height; - gchar *tmp; - - gboolean tv_enabled; - - /* present the window */ - gtk_window_present(GTK_WINDOW(shell->window)); - - /* if the treeview is disabled, we need to enable it so we get the - correct colors when saving. we make it insensitive later on if it - was this way before entering this function */ - tv_enabled = GTK_WIDGET_IS_SENSITIVE(widget); - gtk_widget_set_sensitive(widget, TRUE); - - gtk_widget_queue_draw(widget); - - /* unselect things in the information treeview */ - gtk_range_set_value(GTK_RANGE - (GTK_SCROLLED_WINDOW(shell->info->scroll)-> - vscrollbar), 0.0); - gtk_tree_selection_unselect_all(gtk_tree_view_get_selection - (GTK_TREE_VIEW(widget))); - while (gtk_events_pending()) - gtk_main_iteration(); - - /* initialize stuff */ - gc = gdk_gc_new(widget->window); - gdk_gc_set_background(gc, &black); - gdk_gc_set_foreground(gc, &white); - - context = gtk_widget_get_pango_context(widget); - layout = pango_layout_new(context); - - visible_height = tree_view_get_visible_height(GTK_TREE_VIEW(widget)); - - /* draw the title */ - tmp = g_strdup_printf("<b><big>%s</big></b>\n<small>%s</small>", - shell->selected->name, - shell->selected->notefunc(shell->selected-> - number)); - pango_layout_set_markup(layout, tmp, -1); - pango_layout_set_width(layout, widget->allocation.width * PANGO_SCALE); - pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); - pango_layout_get_pixel_extents(layout, NULL, &rect); - - w = widget->allocation.width; - h = visible_height + rect.height; - - pm = gdk_pixmap_new(widget->window, w, rect.height, -1); - gdk_draw_rectangle(GDK_DRAWABLE(pm), gc, TRUE, 0, 0, w, rect.height); - gdk_draw_layout_with_colors(GDK_DRAWABLE(pm), gc, 0, 0, layout, - &white, &black); - - /* copy the pixmap from the treeview and from the title */ - pb = gdk_pixbuf_get_from_drawable(NULL, - widget->window, - NULL, 0, 0, 0, 0, w, h); - pb = gdk_pixbuf_get_from_drawable(pb, - pm, - NULL, - 0, 0, - 0, visible_height, w, rect.height); - - /* save the pixbuf to a png file */ - gdk_pixbuf_save(pb, filename, "png", NULL, - "compression", "9", - "tEXt::hardinfo::version", VERSION, - "tEXt::hardinfo::arch", ARCH, NULL); - - /* unref */ - g_object_unref(pb); - g_object_unref(layout); - g_object_unref(pm); - g_object_unref(gc); - g_free(tmp); - - gtk_widget_set_sensitive(widget, tv_enabled); -} - static gboolean __idle_free_do(gpointer ptr) { g_free(ptr); @@ -1402,3 +1323,59 @@ gboolean g_strv_contains(const gchar * const * strv, const gchar *str) { return 0; } #endif + +/* Hardinfo labels that have # are truncated and/or hidden. + * Labels can't have $ because that is the delimiter in + * moreinfo. */ +gchar *hardinfo_clean_label(const gchar *v, int replacing) { + gchar *clean, *p; + + p = clean = g_strdup(v); + while (*p != 0) { + switch(*p) { + case '#': case '$': + *p = '_'; + break; + default: + break; + } + p++; + } + if (replacing) + g_free((gpointer)v); + return clean; +} + +/* hardinfo uses the values as {ht,x}ml, apparently */ +gchar *hardinfo_clean_value(const gchar *v, int replacing) { + gchar *clean, *tmp; + gchar **vl; + if (v == NULL) return NULL; + + vl = g_strsplit(v, "&", -1); + if (g_strv_length(vl) > 1) + clean = g_strjoinv("&", vl); + else + clean = g_strdup(v); + g_strfreev(vl); + + vl = g_strsplit(clean, "<", -1); + if (g_strv_length(vl) > 1) { + tmp = g_strjoinv("<", vl); + g_free(clean); + clean = tmp; + } + g_strfreev(vl); + + vl = g_strsplit(clean, ">", -1); + if (g_strv_length(vl) > 1) { + tmp = g_strjoinv(">", vl); + g_free(clean); + clean = tmp; + } + g_strfreev(vl); + + if (replacing) + g_free((gpointer)v); + return clean; +} |