2012-09-08 23 views
2

我完全難倒了。我有這個小東西來緩解在Linux下安裝MTP單元,但由於某些原因我無法獲得libnotify在使用變量時顯示我的圖標。如果我硬編碼完整路徑,它可以正常工作,但使用變量getcwdgetenv時,它不會顯示。notify_notification_new不顯示圖標

這裏是一塊代碼:

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); 
    } 
    } 

我在做什麼錯?

回答

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); 
+0

非常好,這是一種魅力。 – tristan202

+0

起初我雖然notify_notification_new調用是python不是C((%s%sandroid_on.png「%cwd,斜槓)會工作如果它是python :) –

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); 

或者更簡單的,特別是如果你知道你不會溢出字符數組:

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