通过在Ubuntu 16.04中双击打开与GTK +应用程序关联的文件(在C中)

时间:2018-07-10 20:56:22

标签: c gtk ubuntu-16.04 mime

我有一个已编写的GTK + 3程序,它使用GKeyFile来存储用户参数。在执行应用程序时会加载一个默认的密钥文件,该文件存储了以前使用的参数,但是用户可以选择随时将信息存储到扩展名为“ * .dc1”的GKeyFile中,并通过程序打开这些文件。我试图让用户双击扩展名为“ * .dc1”的文件时启动应用程序。我已经将文件与图标相关联,并且还通过创建新的MIME类型将文件与应用程序相关联。 MIME XML说明是:

<?xml version="1.0"?>
 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
   <mime-type type="application/x-dc1">  
   <comment>DC1 Configuration File</comment>
   <glob pattern="*.dc1"/>
  </mime-type>
 </mime-info>

还创建了一个.svg文件形式的图标。然后执行以下命令:

sudo cp ccc_dc1.xml /usr/share/mime/packages
sudo cp application-x-dc1.svg /usr/share/icons/Humanity/mimes
sudo cp application-x-dc1.svg /usr/share/icons/gnome/scalable/mimetypes

运行时会附加MIME类型文件:

sudo -H gedit /etc/mime.types

并添加“ application / x-dc1”。然后使用以下图标将图标添加到文件扩展名:

sudo update-mime-database /usr/share/mime
sudo gtk-update-icon-cache /usr/share/icons/Humanity -f
sudo gtk-update-icon-cache /usr/share/icons/gnome -f

.desktop文件的格式为:

[Desktop Entry]
Version=1.0
Name=DC1
Comment=GUI
Exec=sudo taskset -c 6 /home/DC1/Codes/./CCC_DC1
Icon=/home/DC1/SourceFiles/Images/cytoDC1.png
Terminal=false
Type=Application
Categories=Utility;Application;
MimeType=application/x-dc1;
Name[en_US]=DC1

执行了以下命令:

sudo cp DC1.desktop /usr/share/applications
xdg-mime default DC1.desktop application/x-dc1

完成此操作后,该图标与文件扩展名相关联,但是当我双击带有该扩展名的文件时,该图标将显示在Unity Launcher中,闪烁几次,然后消失。我的理解是,当我通过双击打开文件时,其绝对路径通过argc / argv传递,但是尽管我的Gtk程序以两种方式处理该问题:

  1. 在main中使用gtk_init()时通过处理argv
  2. 使用GtkApplication时使用打开信号
双击文件时,两个实例均失败。但是,在命令行上传递文件名时,两种方法均成功。这使我相信这不是应用程序代码,而是我无法忍受的其他东西。以下是两个Gtk示例。第一种是使用gtk_init:

int main(int argc, char * argv[]) 
{
  GtkWidget * window;

  // Declare read_data of type data and a pointer;
  struct data read_data;

  gtk_init( & argc, & argv);

  read_data.builder = gtk_builder_new();
  read_data.keyfile = g_key_file_new();

  GError * err = NULL;
  GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;

  // Creating a GtkBuilder from a .glade file
  gtk_builder_add_from_file(read_data.builder, GLADE_FILE, & err);

  if (err != NULL) {
    fprintf(stderr, "Unable to read glade file: %s\n", err -> message);
    g_error_free(err);

    return 1;
  }

  // If the program isn't opened by double clicking a key-file, load the previously used parameters
  if (argv[1] == NULL) {
    // Load the GKeyFile from config (.dc1) file
    g_key_file_load_from_file(read_data.keyfile, "/home/DC1/SourceFiles/cfg.dc1", flags, & err);

    if (err != NULL) {
      fprintf(stderr, "Unable to read keyfile: %s\n", err -> message);
      g_error_free(err);

      return 1;
    }
  }
  // If the program was opened by double clicking a key-file, load the parameters from that file instead
  else {
    // Load the GKeyFile from config (.dc1) file
    g_key_file_load_from_file(read_data.keyfile, argv[1], flags, & err);

    printf("The keyfile being loaded has path: %s\n", argv[1]);

    if (err != NULL) {
      fprintf(stderr, "Unable to read keyfile: %s\n", err -> message);
      g_error_free(err);

      return 1;
    }
  }

....AND SO ON
}

第二个是使用GtkApplication:

static void open_callback (GtkApplication *app, gpointer *files, gint n_files, __attribute__((unused)) gchar *hint, 
        __attribute__((unused)) gpointer user_data)
{
  GtkWidget *window;

  // Declare read_data of type data and a pointer;
  struct data read_data;

  read_data.builder = gtk_builder_new();
  read_data.keyfile = g_key_file_new();

  GError * err = NULL;
  GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;

  // Creating a GtkBuilder from a .glade file
  gtk_builder_add_from_file(read_data.builder, GLADE_FILE, & err);

  if (err != NULL) {
    fprintf(stderr, "Unable to read glade file: %s\n", err -> message);
    g_error_free(err);

    return;
  }

  gint i;

  // Open the file via the command line arguments (i.e. by clicking a *.dc1 file associated with the software)
  for (i = 0; i < n_files; i++)
  {
    gchar *filepath = g_file_get_path (files[i]);

    // Load the GKeyFile from config (.dc1) file
    g_key_file_load_from_file (read_data.keyfile, filepath, flags, &err);

    printf("The keyfile being loaded has path: %s\n", filepath);

    if (err != NULL)
    {
      fprintf(stderr, "Unable to read keyfile: %s\n", err->message);
      g_error_free(err);

      return;
    }

    g_free (filepath);
  }

....AND SO ON
}

如果有人对我可能在这里缺少什么有任何建议,我将非常感谢您的帮助。

0 个答案:

没有答案