tell applet is running by counting processes

This commit is contained in:
lins05 2012-08-20 17:45:56 +08:00
parent 2756c8c6ed
commit b82ecc7ea0
7 changed files with 86 additions and 12 deletions

View File

@ -24,6 +24,7 @@ seafile_applet_SOURCES = seafile-applet.c trayicon.c \
seafile_applet_LDADD = @CCNET_LIBS@ \
$(top_builddir)/lib/libseafile.la \
$(top_builddir)/lib/libseafile_common.la \
@GTK_LIBS@ -lssl -lcrypto @LIB_RT@ @LIB_UUID@ \
-lsqlite3 -levent \
$(LIBNOTIFY_LIBS) $(APP_INDICATOR_LIBS) @SEARPC_LIBS@

View File

@ -77,7 +77,7 @@ start_seafile_daemon ()
int ccnet_open_dir(const char *path)
{
char buf[4096];
snprintf (buf, 4096, "nautilus %s &", path);
snprintf (buf, 4096, "nautilus '%s' &", path);
system (buf);
return 0;
}
@ -146,7 +146,7 @@ seafile_applet_init (SeafileApplet *applet)
int
main (int argc, char **argv)
{
if (process_is_running("ccnet")) {
if (count_process("seafile-applet") > 1) {
fprintf(stderr, "Seafile applet already running. I will quit.\n");
exit(1);
}

View File

@ -11,18 +11,15 @@ top_srcdir=../..
GLIB_CFLAGS=$(shell pkg-config --cflags libsearpc)
GLIB_LDFLAGS=$(shell pkg-config --libs libsearpc)
CFLAGS += -I$(top_srcdir)/include -I$(top_srcdir)/include/ccnet \
-I$(top_srcdir)/seafile/include \
CFLAGS += -I$(top_srcdir)/include \
-I$(top_srcdir)/lib \
-I$(top_srcdir)/seafile/lib \
-I. -I../common \
$(GLIB_CFLAGS) -g -O0 -Wall
LDFLAGS += -L${top_srcdir}/lib/.libs/ \
-L$(top_srcdir)/seafile/lib/.libs/ \
-lccnet -lseafile \
-lseafile -lseafile_common -lccnet\
-lcrypto -lgdi32 -levent \
-lRpcrt4 -lpthread -lws2_32 \
-lRpcrt4 -lpthread -lws2_32 -lpsapi \
$(GLIB_LDFLAGS)
CC = gcc
@ -84,7 +81,7 @@ resource.o : ${RCFILE} resource.h ${ICONS} Application.manifest
${RC} -i $< -o $@
$(TARGET) : applet-po-gbk.h $(OBJECTS) resource.o .deps
${CC} -o "$@" ${OBJECTS} resource.o ${LDFLAGS}
${CC} -o "$@" ${OBJECTS} resource.o ${LDFLAGS}
debug:all
distclean:clean

View File

@ -619,8 +619,9 @@ void prompt_win7_tip_if_necessary ()
if (!is_windows_seven () || !first_use)
return;
first_use = FALSE;
DialogBox (applet->hInstance,
MAKEINTRESOURCE(IDD_WIN7_TIP),
applet->hWnd, Win7TipProc);
}

View File

@ -10,11 +10,11 @@
#include <string.h>
#include <ccnet.h>
#include <utils.h>
#include "stdafx.h"
#include "resource.h"
#include "utils.h"
#include "applet-common.h"
#include "trayicon.h"
#include "applet-log.h"
@ -424,7 +424,7 @@ seafile_applet_init (HINSTANCE hInstance)
int
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if (process_is_running("ccnet")) {
if (count_process("seafile-applet") > 1) {
MessageBox(NULL, S_SEAFILE_APPLET_ALREAD_RUNNING, "Seafile", MB_OK);
exit(1);
}

View File

@ -1425,6 +1425,53 @@ get_process_handle (const char *process_name_in)
return NULL;
}
int count_process (const char *process_name_in)
{
char name[MAX_PATH];
char process_name[MAX_PATH];
DWORD aProcesses[1024], cbNeeded, cProcesses;
HANDLE hProcess;
HMODULE hMods[1024];
int count = 0;
int i, j;
if (strstr(process_name_in, ".exe")) {
snprintf (name, sizeof(name), "%s", process_name_in);
} else {
snprintf (name, sizeof(name), "%s.exe", process_name_in);
}
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) {
return 0;
}
/* Calculate how many process identifiers were returned. */
cProcesses = cbNeeded / sizeof(DWORD);
for (i = 0; i < cProcesses; i++) {
if(aProcesses[i] == 0)
continue;
hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
if (!hProcess) {
continue;
}
if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) {
for (j = 0; j < cbNeeded / sizeof(HMODULE); j++) {
if (GetModuleBaseName(hProcess, hMods[j], process_name,
sizeof(process_name))) {
if (strcasecmp(process_name, name) == 0)
count++;
}
}
}
CloseHandle(hProcess);
}
return count;
}
gboolean
process_is_running (const char *process_name)
{
@ -1548,6 +1595,31 @@ gboolean process_is_running (const char *process_name)
closedir(proc_dir);
return FALSE;
}
int count_process(const char *process_name)
{
int count = 0;
DIR *proc_dir = opendir("/proc");
if (!proc_dir) {
g_warning ("failed to open /proc/ :%s\n", strerror(errno));
return FALSE;
}
struct dirent *subdir = NULL;
while ((subdir = readdir(proc_dir))) {
char first = subdir->d_name[0];
/* /proc/[1-9][0-9]* */
if (first > '9' || first < '1')
continue;
if (find_process_in_dirent(subdir, process_name) > 0) {
count++;
}
}
closedir (proc_dir);
return count;
}
#endif
#ifdef __APPLE__

View File

@ -315,6 +315,9 @@ char *ccnet_locale_to_utf8 (const gchar *src);
/* Detect whether a process with the given name is running right now. */
gboolean process_is_running(const char *name);
/* count how much instance of a program is running */
int count_process (const char *process_name_in);
#ifdef WIN32
int win32_kill_process (const char *process_name_in);
int win32_spawn_process (char *cmd, char *wd);