notify_notification_new没有显示图标

时间:2012-09-08 21:51:43

标签: c libnotify

我完全难过了。我有这个小东西来缓解linux下的 MTP 单元的安装,但出于某种原因,我无法在使用变量时显示 libnotify 来显示我的图标。如果我对完整路径进行硬编码,则可以正常工作,但在使用getcwdgetenv变量时,它将无法显示。

以下是一段代码:

char cwd[1024];
char *slash = "/";  
{
NotifyNotification *mount;
notify_init ("Galaxy Nexus mounter");
    if (getcwd(cwd, sizeof(cwd)) != NULL)
    {
        mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", ("%s%sandroid_on.png", cwd, slash));
        fprintf(stdout, "Icon used %s%sandroid_on.png\n", cwd, slash);
        system("jmtpfs ~/Nexus");
        notify_notification_set_timeout (mount, 2000);
        notify_notification_show (mount, NULL);
    }
    }

我做错了什么?

2 个答案:

答案 0 :(得分:1)

("%s%sandroid_on.png", cwd, slash)

您是否知道C中的这种表达等同于?

(slash)

逗号运算符没有其他效果!

您可能希望执行以下操作:

char png[1100];
sprintf(png, "%s%sandroid_on.png", cwd, slash);
mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", png);

或者更简单,特别是如果你知道你不会溢出char数组:

strcat(cwd, slash);
strcat(cwd, "android_on.png");
mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", cwd);

答案 1 :(得分:1)

这看起来并不正确:

mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", ("%s%sandroid_on.png", cwd, slash));

第三个参数应该是一个字符串吗?如果是这样,您需要使用snprintf单独构建它:

char path[1000];
snprintf (path, sizeof(path), "%s%sandroid_on.png", cwd, slash);
mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", path);
fprintf(stdout, "Icon used %s\n", path);