Exemple #1
0
static gboolean
_g_mime_type_is_raw (const char *mime_type)
{
    return (g_content_type_is_a (mime_type, "application/x-crw")	/* ? */
            || g_content_type_is_a (mime_type, "image/x-raw")       /* mimelnk */
            || g_content_type_is_a (mime_type, "image/x-dcraw"));	/* freedesktop.org.xml - this should
									   catch most RAW formats, which are
									   registered as sub-classes of
									   image/x-dcraw */
}
/**
 * g_content_type_can_be_executable:
 * @type: a content type string
 *
 * Checks if a content type can be executable. Note that for instance
 * things like text files can be executables (i.e. scripts and batch files).
 *
 * Returns: %TRUE if the file type corresponds to a type that
 *     can be executable, %FALSE otherwise.
 */
gboolean
g_content_type_can_be_executable (const gchar *type)
{
  g_return_val_if_fail (type != NULL, FALSE);

  if (g_content_type_is_a (type, "application/x-executable")  ||
      g_content_type_is_a (type, "text/plain"))
    return TRUE;

  return FALSE;
}
Exemple #3
0
static gboolean
_g_mime_type_is_hdr (const char *mime_type)
{
    /* Note that some HDR file extensions have been hard-coded into
       the get_file_mime_type function above. */
    return g_content_type_is_a (mime_type, "image/x-hdr");
}
static void
gth_metadata_provider_gstreamer_read (GthMetadataProvider *self,
				      GthFileData         *file_data,
				      const char          *attributes,
				      GCancellable        *cancellable)
{
	if (! g_content_type_is_a (gth_file_data_get_mime_type (file_data), "audio/*")
	    && ! g_content_type_is_a (gth_file_data_get_mime_type (file_data), "video/*"))
	{
		return;
	}

	/* this function is executed in a secondary thread, so calling
	 * slow sync functions is not a problem. */

	gstreamer_read_metadata_from_file (file_data->file, file_data->info, NULL);
}
static void
process (int argc,
         char **argv)
{
  GList *svg_files = NULL;
  GQueue *descend_into_files;
  GFile *current_dir, *symbolic_theme, *file;
  gchar *str;
  gint idx;

  str = g_get_current_dir ();
  current_dir = g_file_new_for_path (str);
  g_free (str);

  symbolic_theme = g_file_new_for_commandline_arg (argv[1]);
  gnome_dir = g_file_resolve_relative_path (symbolic_theme, "gnome");
  hc_dir = g_file_resolve_relative_path (current_dir, "icons");
  g_object_unref (symbolic_theme);

  descend_into_files = g_queue_new ();
  g_queue_push_tail (descend_into_files, g_object_ref (gnome_dir));
  while ((file = g_queue_pop_head (descend_into_files)) != NULL)
    {
      GFileInfo *child_info;
      GFileEnumerator *enumerator = 
        g_file_enumerate_children (file, "standard::name,standard::type,standard::content-type", 
                                   G_FILE_QUERY_INFO_NONE, NULL, NULL);

      while ((child_info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
        {
          if (g_file_info_get_file_type (child_info) == G_FILE_TYPE_DIRECTORY)
            g_queue_push_tail (descend_into_files, g_file_resolve_relative_path (file, g_file_info_get_name (child_info)));
          else if (g_content_type_is_a (g_file_info_get_content_type (child_info), "image/svg+xml"))
            svg_files = g_list_prepend (svg_files, g_file_resolve_relative_path (file, g_file_info_get_name (child_info)));

          g_object_unref (child_info);
        }

      g_object_unref (enumerator);
      g_object_unref (file);
    }

  for (idx = 0; idx < G_N_ELEMENTS (icon_sizes); idx++)
    write_png_theme (svg_files, icon_sizes[idx]);

  g_list_free_full (svg_files, g_object_unref);
  g_queue_free (descend_into_files);
  g_clear_object (&gnome_dir);
  g_clear_object (&hc_dir);
}
static gboolean
is_image (const char *url)
{
	char *content_type;
	gboolean retval = FALSE;

	content_type = g_content_type_guess (url, NULL, 0, NULL);
	if (content_type == NULL)
		return FALSE;
	if (g_content_type_is_a (content_type, "image/*"))
		retval = TRUE;
	g_free (content_type);
	return retval;
}
static GtkSourceCompressionType
get_compression_type_from_content_type (const gchar *content_type)
{
	if (content_type == NULL)
	{
		return GTK_SOURCE_COMPRESSION_TYPE_NONE;
	}

	if (g_content_type_is_a (content_type, "application/x-gzip"))
	{
		return GTK_SOURCE_COMPRESSION_TYPE_GZIP;
	}

	return GTK_SOURCE_COMPRESSION_TYPE_NONE;
}
Exemple #8
0
GeditDocumentCompressionType
gedit_utils_get_compression_type_from_content_type (const gchar *content_type)
{
	if (content_type == NULL)
	{
		return GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE;
	}

	if (g_content_type_is_a (content_type, "application/x-gzip"))
	{
		return GEDIT_DOCUMENT_COMPRESSION_TYPE_GZIP;
	}

	return GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE;
}
static gboolean
content_type_is_native (const gchar *content_type)
{
  gchar **native_types;
  gint idx;
  gboolean found = FALSE;

  native_types = nemo_preview_query_supported_document_types ();

  for (idx = 0; native_types[idx] != NULL; idx++) {
    found = g_content_type_is_a (content_type, native_types[idx]);
    if (found)
      break;
  }

  g_strfreev (native_types);

  return found;
}
gboolean
activate_file (GFile* file, const char* content_type,
               gboolean is_executable, GFile* _file_arg)
{
    char* file_name = g_file_get_basename (file);
    gboolean is_bin = g_str_has_suffix(file_name, ".bin");
    gboolean result = TRUE;

    g_debug ("activate_file: %s", file_name);
    g_free (file_name);
    g_debug ("content_type: %s", content_type);

    if (is_executable &&
        (g_content_type_can_be_executable (content_type) || is_bin))
    {
        g_debug("is_executable && g_content_type_can_be_executable || is_bin");
        //1. an executable text file. or an shell script
        if (g_content_type_is_a (content_type, "text/plain"))
        {
            g_debug("g_content_type_is_a");
            GtkWidget* dialog;
            int response;
            char* file_name;
            char* prompt;
            char* detail;

            file_name = g_file_get_basename (file);
            prompt = g_strdup_printf (_("Do you want to run \"%s\", or display its contents?"), file_name);
            detail = g_strdup_printf (_("\"%s\" is an executable text file."), file_name);
            g_free (file_name);
            //create prompt dialog
            dialog = gtk_message_dialog_new (NULL,
                                             GTK_DIALOG_MODAL,
                                             GTK_MESSAGE_QUESTION,
                                             GTK_BUTTONS_NONE,
                                             NULL);
            g_object_set (dialog, "text", prompt, "secondary-text", detail, NULL);
            g_free (prompt);
            g_free (detail);
            gtk_dialog_add_button (GTK_DIALOG(dialog), _("Run in _Terminal"), RESPONSE_RUN_IN_TERMINAL);
            gtk_dialog_add_button (GTK_DIALOG(dialog), _("_Display"), RESPONSE_DISPLAY);
            gtk_dialog_add_button (GTK_DIALOG(dialog), "_Cancel", GTK_RESPONSE_CANCEL);
            gtk_dialog_add_button (GTK_DIALOG(dialog), _("_Run"), RESPONSE_RUN);
            gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);

            gtk_widget_show (GTK_WIDGET (dialog));

            response = gtk_dialog_run (GTK_DIALOG(dialog));
            gtk_widget_destroy (GTK_WIDGET (dialog));

            g_message("response:%d",response);
            switch (response)
            {
            case RESPONSE_RUN_IN_TERMINAL:
                run_file_in_terminal (file);
                break;
            case RESPONSE_DISPLAY:
                result = display_file (file, content_type);
                break;
            case RESPONSE_RUN:
                run_file (file, NULL);
                break;
            case GTK_RESPONSE_CANCEL:
                break;
            default:
                break;
            }
        }
        //2. an executable binary file
        else
        {
            g_debug("run_file");
            run_file (file, _file_arg);
        }
    }
    //for non-executables just open it.
    else
    {
        g_debug("for non-executables just open it.");
        result = display_file (file, content_type);
    }

    return result;
}
Exemple #11
0
static void prv_compute_mime_and_dlna_header(const gchar *filename,
					     gchar **mime_type,
					     gchar **dlna_header,
					     GError **error)
{
	gchar *uri;
	GString *header;
	GUPnPDLNAProfile *profile;
	GUPnPDLNAProfileGuesser *guesser;
	gboolean relaxed_mode = TRUE;
	gboolean extended_mode = TRUE;
	const char *profile_name;
	const char *dlna_mime_type;
	GUPnPDLNAOperation operation;
	GUPnPDLNAFlags flags;
	GUPnPDLNAConversion conversion;
	gchar *content_type = NULL;

	*mime_type = NULL;

	*dlna_header = NULL;

	*error = NULL;

	header = g_string_new("");

	guesser = gupnp_dlna_profile_guesser_new(relaxed_mode, extended_mode);

	uri = g_filename_to_uri(filename, NULL, error);
	if (uri == NULL) {
		DLEYNA_LOG_WARNING("Unable to convert filename: %s", filename);

		if (*error) {
			DLEYNA_LOG_WARNING("Error: %s", (*error)->message);

			g_error_free(*error);
			*error = NULL;
		}

		goto on_error;
	}

	profile = gupnp_dlna_profile_guesser_guess_profile_sync(guesser,
								uri,
								5000,
								NULL,
								error);
	if (profile == NULL) {
		DLEYNA_LOG_WARNING("Unable to guess profile for URI: %s", uri);

		if (*error) {
			DLEYNA_LOG_WARNING("Error: %s", (*error)->message);

			g_error_free(*error);
			*error = NULL;
		}

		goto on_error;
	}

	profile_name = gupnp_dlna_profile_get_name(profile);
	if (profile_name != NULL)
		g_string_append_printf(header, "DLNA.ORG_PN=%s;", profile_name);

	operation = GUPNP_DLNA_OPERATION_RANGE;
	g_string_append_printf(header, "DLNA.ORG_OP=%.2x;", operation);

	conversion = GUPNP_DLNA_CONVERSION_NONE;
	g_string_append_printf(header, "DLNA.ORG_CI=%.2x;", conversion);

	dlna_mime_type = gupnp_dlna_profile_get_mime(profile);
	if (dlna_mime_type != NULL) {
		*mime_type = g_strdup(dlna_mime_type);

		flags = GUPNP_DLNA_FLAGS_BACKGROUND_TRANSFER_MODE;
		flags |= GUPNP_DLNA_FLAGS_CONNECTION_STALL;
		flags |= GUPNP_DLNA_FLAGS_DLNA_V15;

		if (g_content_type_is_a(dlna_mime_type, "image/*")) {
			flags |= GUPNP_DLNA_FLAGS_INTERACTIVE_TRANSFER_MODE;
		} else if (g_content_type_is_a(dlna_mime_type, "audio/*") ||
			   g_content_type_is_a(dlna_mime_type, "video/*")) {
			flags |= GUPNP_DLNA_FLAGS_STREAMING_TRANSFER_MODE;
		} else {
			DLEYNA_LOG_WARNING("Unsupported Mime Type: %s",
					   dlna_mime_type);

			goto on_error;
		}

		g_string_append_printf(header, "DLNA.ORG_FLAGS=%.8x", flags);
		g_string_append_printf(header, "000000000000000000000000");
	} else {
		DLEYNA_LOG_WARNING("Unable to discover mime_type");
	}

on_error:

	if (*mime_type == NULL) {
		content_type = g_content_type_guess(filename, NULL, 0, NULL);

		if (content_type != NULL) {
			*mime_type = g_content_type_get_mime_type(content_type);

			if (*mime_type == NULL)
				*error = g_error_new(DLEYNA_SERVER_ERROR,
						     DLEYNA_ERROR_BAD_MIME,
						     "Unable to determine MIME Type for %s",
						     filename);

			g_free(content_type);
		} else {
			*error = g_error_new(DLEYNA_SERVER_ERROR,
					     DLEYNA_ERROR_BAD_MIME,
					     "Unable to determine Content Type for %s",
					     filename);
		}
	}

	DLEYNA_LOG_DEBUG("contentFeatures.dlna.org: %s", header->str);

	g_object_unref(guesser);

	g_free(uri);

	if (*mime_type)
		*dlna_header = g_string_free(header, FALSE);
	else
		(void) g_string_free(header, TRUE);

	return;
}
/* returns TRUE if a folder window should be opened */
static gboolean
do_autorun_for_content_type (GMount *mount, const char *x_content_type, CajaAutorunOpenWindow open_window_func, gpointer user_data)
{
    AutorunDialogData *data;
    GtkWidget *dialog;
    GtkWidget *hbox;
    GtkWidget *vbox;
    GtkWidget *label;
    GtkWidget *combo_box;
    GtkWidget *always_check_button;
    GtkWidget *eject_button;
    GtkWidget *image;
    char *markup;
    char *content_description;
    char *mount_name;
    GIcon *icon;
    GdkPixbuf *pixbuf;
    CajaIconInfo *icon_info;
    int icon_size;
    gboolean user_forced_dialog;
    gboolean pref_ask;
    gboolean pref_start_app;
    gboolean pref_ignore;
    gboolean pref_open_folder;
    char *media_greeting;
    gboolean ret;

    ret = FALSE;
    mount_name = NULL;

    if (g_content_type_is_a (x_content_type, "x-content/win32-software"))
    {
        /* don't pop up the dialog anyway if the content type says
         * windows software.
         */
        goto out;
    }

    user_forced_dialog = is_shift_pressed ();

    caja_autorun_get_preferences (x_content_type, &pref_start_app, &pref_ignore, &pref_open_folder);
    pref_ask = !pref_start_app && !pref_ignore && !pref_open_folder;

    if (user_forced_dialog)
    {
        goto show_dialog;
    }

    if (!pref_ask && !pref_ignore && !pref_open_folder)
    {
        GAppInfo *app_info;
        app_info = g_app_info_get_default_for_type (x_content_type, FALSE);
        if (app_info != NULL)
        {
            caja_autorun_launch_for_mount (mount, app_info);
        }
        goto out;
    }

    if (pref_open_folder)
    {
        ret = TRUE;
        goto out;
    }

    if (pref_ignore)
    {
        goto out;
    }

show_dialog:

    mount_name = g_mount_get_name (mount);

    dialog = gtk_dialog_new ();

    hbox = gtk_hbox_new (FALSE, 12);
    gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox, TRUE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);

    icon = g_mount_get_icon (mount);
    icon_size = caja_get_icon_size_for_stock_size (GTK_ICON_SIZE_DIALOG);
    icon_info = caja_icon_info_lookup (icon, icon_size);
    pixbuf = caja_icon_info_get_pixbuf_at_size (icon_info, icon_size);
    g_object_unref (icon_info);
    g_object_unref (icon);
    image = gtk_image_new_from_pixbuf (pixbuf);
    gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
    gtk_box_pack_start (GTK_BOX (hbox), image, TRUE, TRUE, 0);
    /* also use the icon on the dialog */
    gtk_window_set_title (GTK_WINDOW (dialog), mount_name);
    gtk_window_set_icon (GTK_WINDOW (dialog), pixbuf);
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
    g_object_unref (pixbuf);

    vbox = gtk_vbox_new (FALSE, 12);
    gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);

    label = gtk_label_new (NULL);


    /* Customize greeting for well-known x-content types */
    if (strcmp (x_content_type, "x-content/audio-cdda") == 0)
    {
        media_greeting = _("You have just inserted an Audio CD.");
    }
    else if (strcmp (x_content_type, "x-content/audio-dvd") == 0)
    {
        media_greeting = _("You have just inserted an Audio DVD.");
    }
    else if (strcmp (x_content_type, "x-content/video-dvd") == 0)
    {
        media_greeting = _("You have just inserted a Video DVD.");
    }
    else if (strcmp (x_content_type, "x-content/video-vcd") == 0)
    {
        media_greeting = _("You have just inserted a Video CD.");
    }
    else if (strcmp (x_content_type, "x-content/video-svcd") == 0)
    {
        media_greeting = _("You have just inserted a Super Video CD.");
    }
    else if (strcmp (x_content_type, "x-content/blank-cd") == 0)
    {
        media_greeting = _("You have just inserted a blank CD.");
    }
    else if (strcmp (x_content_type, "x-content/blank-dvd") == 0)
    {
        media_greeting = _("You have just inserted a blank DVD.");
    }
    else if (strcmp (x_content_type, "x-content/blank-cd") == 0)
    {
        media_greeting = _("You have just inserted a blank Blu-Ray disc.");
    }
    else if (strcmp (x_content_type, "x-content/blank-cd") == 0)
    {
        media_greeting = _("You have just inserted a blank HD DVD.");
    }
    else if (strcmp (x_content_type, "x-content/image-photocd") == 0)
    {
        media_greeting = _("You have just inserted a Photo CD.");
    }
    else if (strcmp (x_content_type, "x-content/image-picturecd") == 0)
    {
        media_greeting = _("You have just inserted a Picture CD.");
    }
    else if (strcmp (x_content_type, "x-content/image-dcf") == 0)
    {
        media_greeting = _("You have just inserted a medium with digital photos.");
    }
    else if (strcmp (x_content_type, "x-content/audio-player") == 0)
    {
        media_greeting = _("You have just inserted a digital audio player.");
    }
    else if (g_content_type_is_a (x_content_type, "x-content/software"))
    {
        media_greeting = _("You have just inserted a medium with software intended to be automatically started.");
    }
    else
    {
        /* fallback to generic greeting */
        media_greeting = _("You have just inserted a medium.");
    }
    markup = g_strdup_printf ("<big><b>%s %s</b></big>", media_greeting, _("Choose what application to launch."));
    gtk_label_set_markup (GTK_LABEL (label), markup);
    g_free (markup);
    gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
    gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);

    label = gtk_label_new (NULL);
    content_description = g_content_type_get_description (x_content_type);
    markup = g_strdup_printf (_("Select how to open \"%s\" and whether to perform this action in the future for other media of type \"%s\"."), mount_name, content_description);
    g_free (content_description);
    gtk_label_set_markup (GTK_LABEL (label), markup);
    g_free (markup);
    gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
    gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);

    data = g_new0 (AutorunDialogData, 1);
    data->dialog = dialog;
    data->mount = g_object_ref (mount);
    data->remember = !pref_ask;
    data->selected_ignore = pref_ignore;
    data->x_content_type = g_strdup (x_content_type);
    data->selected_app = g_app_info_get_default_for_type (x_content_type, FALSE);
    data->open_window_func = open_window_func;
    data->user_data = user_data;

    combo_box = gtk_combo_box_new ();
    caja_autorun_prepare_combo_box (combo_box, x_content_type, FALSE, TRUE, FALSE, autorun_combo_changed, data);
    g_signal_connect (G_OBJECT (combo_box),
                      "key-press-event",
                      G_CALLBACK (combo_box_enter_ok),
                      dialog);

    gtk_box_pack_start (GTK_BOX (vbox), combo_box, TRUE, TRUE, 0);

    always_check_button = gtk_check_button_new_with_mnemonic (_("_Always perform this action"));
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (always_check_button), data->remember);
    g_signal_connect (G_OBJECT (always_check_button),
                      "toggled",
                      G_CALLBACK (autorun_always_toggled),
                      data);
    gtk_box_pack_start (GTK_BOX (vbox), always_check_button, TRUE, TRUE, 0);

    gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                            GTK_STOCK_OK, GTK_RESPONSE_OK,
                            NULL);
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);

    if (g_mount_can_eject (mount))
    {
        GtkWidget *eject_image;
        eject_button = gtk_button_new_with_mnemonic (_("_Eject"));
        pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                           "media-eject",
                                           caja_get_icon_size_for_stock_size (GTK_ICON_SIZE_BUTTON),
                                           0,
                                           NULL);
        eject_image = gtk_image_new_from_pixbuf (pixbuf);
        g_object_unref (pixbuf);
        gtk_button_set_image (GTK_BUTTON (eject_button), eject_image);
        data->should_eject = TRUE;
    }
    else
    {
        eject_button = gtk_button_new_with_mnemonic (_("_Unmount"));
        data->should_eject = FALSE;
    }
    gtk_dialog_add_action_widget (GTK_DIALOG (dialog), eject_button, AUTORUN_DIALOG_RESPONSE_EJECT);
    gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (gtk_dialog_get_action_area (GTK_DIALOG (dialog))), eject_button, TRUE);

    /* show the dialog */
    gtk_widget_show_all (dialog);

    g_signal_connect (G_OBJECT (dialog),
                      "response",
                      G_CALLBACK (autorun_dialog_response),
                      data);

    g_signal_connect (G_OBJECT (data->mount),
                      "unmounted",
                      G_CALLBACK (autorun_dialog_mount_unmounted),
                      data);

out:
    g_free (mount_name);
    return ret;
}
int content_type_is(char* type, char* expected_type)
{
    return g_content_type_is_a(type, expected_type);
}
Exemple #14
0
gboolean
gitg_utils_can_display_content_type(gchar const *content_type)
{
    return g_content_type_is_a(content_type, "text/plain") ||
           g_content_type_equals(content_type, "application/octet-stream");
}
static gboolean
log_load (GIOSchedulerJob *io_job,
          GCancellable *cancellable,
          gpointer user_data)
{
  /* this runs in a separate i/o thread */
  LoadJob *job = user_data;
  LogviewLog *log = job->log;
  GFile *f = log->priv->file;
  GFileInfo *info;
  GInputStream *is;
  const char *peeked_buffer;
  const char * parse_data[2];
  GSList *days;
  const char *content_type;
  GFileType type;
  GError *err = NULL;
  GTimeVal timeval;
  gboolean is_archive, can_read;

  info = g_file_query_info (f,
                            G_FILE_ATTRIBUTE_ACCESS_CAN_READ ","
                            G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
                            G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
                            G_FILE_ATTRIBUTE_STANDARD_TYPE ","
                            G_FILE_ATTRIBUTE_STANDARD_SIZE ","
                            G_FILE_ATTRIBUTE_TIME_MODIFIED ",",
                            0, NULL, &err);
  if (err) {
    if (err->code == G_IO_ERROR_PERMISSION_DENIED) {
      /* TODO: PolicyKit integration */
    }
    goto out;
  }

  can_read = g_file_info_get_attribute_boolean (info,
                                                G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
  if (!can_read) {
    /* TODO: PolicyKit integration */
    err = g_error_new_literal (LOGVIEW_ERROR_QUARK, LOGVIEW_ERROR_PERMISSION_DENIED,
                               _("You don't have enough permissions to read the file."));
    g_object_unref (info);

    goto out;
  }

  type = g_file_info_get_file_type (info);
  content_type = g_file_info_get_content_type (info);

  is_archive = g_content_type_equals (content_type, "application/x-gzip");

  if (type != (G_FILE_TYPE_REGULAR || G_FILE_TYPE_SYMBOLIC_LINK) ||
      (!g_content_type_is_a (content_type, "text/plain") && !is_archive))
  {
    err = g_error_new_literal (LOGVIEW_ERROR_QUARK, LOGVIEW_ERROR_NOT_A_LOG,
                               _("The file is not a regular file or is not a text file."));
    g_object_unref (info);

    goto out;
  }

  log->priv->file_size = g_file_info_get_size (info);
  g_file_info_get_modification_time (info, &timeval);
  log->priv->file_time = timeval.tv_sec;
  log->priv->display_name = g_strdup (g_file_info_get_display_name (info));

  g_object_unref (info);

  /* initialize the stream */
  is = G_INPUT_STREAM (g_file_read (f, NULL, &err));

  if (err) {
    if (err->code == G_IO_ERROR_PERMISSION_DENIED) {
      /* TODO: PolicyKit integration */
    }

    goto out;
  }

  if (is_archive) {
#ifdef HAVE_ZLIB
    GZHandle *gz;
    gboolean res;
    guchar * buffer;
    gsize bytes_read;
    GInputStream *real_is;
    time_t mtime; /* seconds */

    /* this also skips the header from |is| */
    res = read_gzip_header (is, &mtime);

    if (!res) {
      g_object_unref (is);

      err = create_zlib_error ();
      goto out;
    }

    log->priv->file_time = mtime;

    gz = gz_handle_new (f, is);
    res = gz_handle_init (gz);

    if (!res) {
      g_object_unref (is);
      gz_handle_free (gz);

      err = create_zlib_error ();
      goto out;
    }

    real_is = g_memory_input_stream_new ();

    do {
      buffer = g_malloc (1024);
      res = gz_handle_read (gz, buffer, 1024, &bytes_read);
      g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (real_is),
                                      buffer, bytes_read, g_free);
    } while (res == TRUE && bytes_read > 0);

    if (!res) {
      gz_handle_free (gz);
      g_object_unref (real_is);
      g_object_unref (is);

      err = create_zlib_error ();
      goto out;
    }
 
    g_object_unref (is);
    is = real_is;

    gz_handle_free (gz);
#else /* HAVE_ZLIB */
    g_object_unref (is);

    err = g_error_new_literal (LOGVIEW_ERROR_QUARK, LOGVIEW_ERROR_NOT_SUPPORTED,
                               _("This version of System Log does not support GZipped logs."));
    goto out;
#endif /* HAVE_ZLIB */
  }

  log->priv->stream = g_data_input_stream_new (is);

  /* sniff into the stream for a timestamped line */
  g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (log->priv->stream),
                                (gssize) g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (log->priv->stream)),
                                NULL, &err);
  if (err == NULL) {
    peeked_buffer = g_buffered_input_stream_peek_buffer
        (G_BUFFERED_INPUT_STREAM (log->priv->stream), NULL);
    parse_data[0] = peeked_buffer;
    parse_data[1] = NULL;

    if ((days = log_read_dates (parse_data, time (NULL))) != NULL) {
      log->priv->has_days = TRUE;
      g_slist_foreach (days, (GFunc) logview_utils_day_free, NULL);
      g_slist_free (days);
    } else {
      log->priv->has_days = FALSE;
    }
  } else {
    log->priv->has_days = FALSE;
    g_clear_error (&err);
  }

  g_object_unref (is);

out:
  if (err) {
    job->err = err;
  }

  g_io_scheduler_job_send_to_mainloop_async (io_job,
                                             log_load_done,
                                             job, NULL);
  return FALSE;
}
Exemple #16
0
static void
enumerate_next_files_async_cb (GObject *source,
                               GAsyncResult *res,
                               gpointer user_data)
{
  EnumerateJob *job = user_data;
  GList *enumerated_files, *l;
  GFileInfo *info;
  GSList *logs;
  const char *content_type, *name;
  char *parse_string, *container_path;
  GFileType type;
  GFile *container;

  enumerated_files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source),
                                                          res, NULL);
  if (!enumerated_files) {
    enumerate_job_finish (job);
    return;
  }

  logs = job->logs;
  container = g_file_enumerator_get_container (G_FILE_ENUMERATOR (source));
  container_path = g_file_get_path (container);

  /* TODO: we don't support grouping rotated logs yet, skip gzipped files
   * and those which name contains a formatted date.
   */
  for (l = enumerated_files; l; l = l->next) {
    info = l->data;
    type = g_file_info_get_file_type (info);
    content_type = g_file_info_get_content_type (info);
    name = g_file_info_get_name (info);
    
    if (!g_file_info_get_attribute_boolean (info, "access::can-read")) {
      g_object_unref (info);
      continue;
    }

    if (type != (G_FILE_TYPE_REGULAR || G_FILE_TYPE_SYMBOLIC_LINK) ||
        !g_content_type_is_a (content_type, "text/plain"))
    {
      g_object_unref (info);
      continue;
    }

    if (g_content_type_is_a (content_type, "application/x-gzip")) {
      g_object_unref (info);
      continue;
    }

    if (g_regex_match_simple ("\\d{8}$", name, 0, 0)) {
      g_object_unref (info);
      continue;
    }

    parse_string = g_build_filename (container_path, name, NULL);

    if (g_slist_find_custom (logs, parse_string, (GCompareFunc) g_ascii_strcasecmp) == NULL) {
      logs = g_slist_append (logs, parse_string);
    } else {
      g_free (parse_string);
    }

    g_object_unref (info);
    parse_string = NULL;
  }

  g_list_free (enumerated_files);
  g_object_unref (container);
  g_free (container_path);

  job->logs = logs;

  enumerate_job_finish (job);
}
Exemple #17
0
static gboolean prv_compute_mime_and_class(dls_task_t *task,
					   dls_async_upload_t *cb_task_data,
					   GError **error)
{
	gchar *content_type = NULL;

	if (!g_file_test(task->ut.upload.file_path,
			 G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) {
		DLEYNA_LOG_WARNING(
			"File %s does not exist or is not a regular file",
			task->ut.upload.file_path);

		*error = g_error_new(DLEYNA_SERVER_ERROR,
				     DLEYNA_ERROR_OBJECT_NOT_FOUND,
				     "File %s does not exist or is not a regular file",
				     task->ut.upload.file_path);
		goto on_error;
	}

	content_type = g_content_type_guess(task->ut.upload.file_path, NULL, 0,
					    NULL);

	if (!content_type) {
		DLEYNA_LOG_WARNING("Unable to determine Content Type for %s",
				   task->ut.upload.file_path);

		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unable to determine Content Type for %s",
				     task->ut.upload.file_path);
		goto on_error;
	}

	cb_task_data->mime_type = g_content_type_get_mime_type(content_type);
	g_free(content_type);

	if (!cb_task_data->mime_type) {
		DLEYNA_LOG_WARNING("Unable to determine MIME Type for %s",
				   task->ut.upload.file_path);

		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unable to determine MIME Type for %s",
				     task->ut.upload.file_path);
		goto on_error;
	}

	if (g_content_type_is_a(cb_task_data->mime_type, "image/*")) {
		cb_task_data->object_class = "object.item.imageItem";
	} else if (g_content_type_is_a(cb_task_data->mime_type, "audio/*")) {
		cb_task_data->object_class = "object.item.audioItem";
	} else if (g_content_type_is_a(cb_task_data->mime_type, "video/*")) {
		cb_task_data->object_class = "object.item.videoItem";
	} else {
		DLEYNA_LOG_WARNING("Unsupported MIME Type %s",
				   cb_task_data->mime_type);

		*error = g_error_new(DLEYNA_SERVER_ERROR, DLEYNA_ERROR_BAD_MIME,
				     "Unsupported MIME Type %s",
				     cb_task_data->mime_type);
		goto on_error;
	}

	return TRUE;

on_error:

	return FALSE;
}
Exemple #18
0
static void
render_card_init (char *card_fname)
{
	int i;
	if (render_init) {
		for (i = 0; i < 52; i++)
			g_object_unref (card_pixbuf[i]);
		cairo_surface_destroy (grey_surface);
		render_init = 0;
	}

	/* gdk_pixbuf_new_from_file doesn't seem to support .svgz (while
	 * librsvg does), so decompress it here. Code from aisleriot
	 * src/lib/ar-svg.c */
	GFile *cf = g_file_new_for_path (card_fname);
	GFileInfo *info;
	GError *error = NULL;
	if (!(info = g_file_query_info (cf,
					G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
					G_FILE_QUERY_INFO_NONE,
					NULL,
					&error))) {
		printf ("%s: %s\n", card_fname, error->message);
		g_object_unref (cf);
		g_error_free (error);
		return;
	}

	const char *type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
	char *gz_type = g_content_type_from_mime_type ("application/x-gzip");
	gboolean is_gzip = (type != NULL && g_content_type_is_a (type, gz_type));
	g_free (gz_type);
	g_object_unref (info);

	GInputStream *stream;
	if (!(stream = G_INPUT_STREAM (g_file_read (cf, NULL, &error)))) {
		printf ("%s: %s\n", card_fname, error->message);
		g_object_unref (cf);
		g_error_free (error);
		return;
	}
	g_object_unref (cf);

	if (is_gzip) {
		GZlibDecompressor *decompressor;
		GInputStream *converter_stream;

		decompressor = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP);
		converter_stream = g_converter_input_stream_new (stream,
				G_CONVERTER (decompressor));
		g_object_unref (stream);
		stream = converter_stream;
	}

	/* file contains cards in 13 columns (A/2..10/J/Q/K) and 5 rows (C/D/H/S/Jokers) */
	/* actual card height is computed from resulting actual size */
	GdkPixbuf *pb = gdk_pixbuf_new_from_stream_at_scale (stream,
			card_width * 13, -1, TRUE, NULL, &error);
	g_object_unref (stream);
	if (!pb) {
		printf ("%s: %s.\n", card_fname, error->message);
		g_error_free (error);
		return;
	}
	int buf_width = gdk_pixbuf_get_width (pb);
	int buf_height = gdk_pixbuf_get_height (pb);
	card_width = ceil (gdk_pixbuf_get_width (pb) / 13.0);
	card_height = ceil (gdk_pixbuf_get_height (pb) / 5.0);
	for (i = 0; i < 52; i++) {
		card_pixbuf[i] = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, card_width, card_height);
		if (!card_pixbuf[i]) {
			printf ("%s: rendering card_pixbuf failed\n", card_fname);
			return;
		}
		int col = (i + 1) % 13;
		int row = i / 13;
		gdk_pixbuf_copy_area (pb, buf_width * col / 13.0, buf_height * row / 5.0,
		//gdk_pixbuf_copy_area (pb, card_width * col, card_height * row,
			card_width, card_height, card_pixbuf[i], 0, 0);
	}
	g_object_unref (pb);

	/* construct a alpha channel in card shape for greying out cards */
	grey_surface = cairo_image_surface_create (CAIRO_FORMAT_A8, card_width, card_height);
	cairo_t *ct = cairo_create (grey_surface);
	gdk_cairo_set_source_pixbuf (ct, card_pixbuf[0], 0, 0);
	cairo_paint_with_alpha (ct, 0.3);
	cairo_destroy (ct);

	render_init = 1;
}
Exemple #19
0
static VALUE
rg_s_is_a_p(G_GNUC_UNUSED VALUE self, VALUE arg1, VALUE arg2)
{
        return CBOOL2RVAL(g_content_type_is_a(RVAL2CSTR(arg1), RVAL2CSTR(arg2)));
}
Exemple #20
0
static void
add_data_tab (const gchar *demoname)
{
  gchar *resource_dir, *resource_name, *content_type, *content_mime;
  gchar **resources;
  GBytes *bytes;
  GtkWidget *widget, *label;
  guint i;

  resource_dir = g_strconcat ("/", demoname, NULL);
  resources = g_resources_enumerate_children (resource_dir, 0, NULL);
  if (resources == NULL)
    {
      g_free (resource_dir);
      return;
    }

  for (i = 0; resources[i]; i++)
    {
      resource_name = g_strconcat (resource_dir, "/", resources[i], NULL);
      bytes = g_resources_lookup_data (resource_name, 0, NULL);
      g_assert (bytes);

      content_type = g_content_type_guess (resource_name,
                                           g_bytes_get_data (bytes, NULL),
                                           g_bytes_get_size (bytes),
                                           NULL);
      content_mime = g_content_type_get_mime_type (content_type);

      /* In theory we should look at all the mime types gdk-pixbuf supports
       * and go from there, but we know what file types we've added.
       */
      if (g_content_type_is_a (content_mime, "image/png") ||
          g_content_type_is_a (content_mime, "image/gif") ||
          g_content_type_is_a (content_mime, "image/jpeg"))
        {
          widget = gtk_image_new_from_resource (resource_name);
        }
      else if (g_content_type_is_a (content_mime, "text/plain") ||
               g_content_type_is_a (content_mime, "application/x-ext-ui") ||
               g_content_type_is_a (content_mime, "text/css"))
        {
          GtkTextBuffer *buffer;
          GtkWidget *textview;

          widget = create_text (&textview, FALSE);
          buffer = gtk_text_buffer_new (NULL);
          gtk_text_buffer_set_text (buffer, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
          gtk_text_view_set_buffer (GTK_TEXT_VIEW (textview), buffer);
        }
      else
        {

          g_warning ("Don't know how to display resource '%s' of type '%s'\n", resource_name, content_mime);
          widget = NULL;
        }

      gtk_widget_show_all (widget);
      label = gtk_label_new (resources[i]);
      gtk_widget_show (label);
      gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
      gtk_container_child_set (GTK_CONTAINER (notebook),
                               GTK_WIDGET (widget),
                               "tab-expand", TRUE,
                               NULL);

      g_free (content_mime);
      g_free (content_type);
      g_free (resource_name);
      g_bytes_unref (bytes);
    }

  g_strfreev (resources);
  g_free (resource_dir);
}