#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
#include <dbus/dbus.h>
#include <stdlib.h>
#include <dbus/dbus-glib-lowlevel.h> //for mainloop
#include <glib-2.0/glib.h>
#include <dbus/dbus-glib.h>
#include <libnotify/notification.h>
#include <libnotify/notify.h>
#include <libnotify/notify-enum-types.h>
#include <libnotify/notify-features.h>
#include <libintl.h>//gettext

enum {
    MM_OPEN_SUCESS  = 1,
    MM_OPEN_FAILED  = 2,
	MM_CLOSE_SUCESS = 3,
	MM_CLOSE_FAILED = 4
};

GMainLoop* mainloop;

int notifications(char *sigvalue)
{
	NotifyNotification* notification;
	gboolean            success;
	GError*             error = NULL;
	int reply_result = -1;
	if (!notify_init (gettext(" ")))//主题
		return 1;

	notification = notify_notification_new (
				gettext("运维模式状态"),//标题
				gettext(sigvalue),//内容
				"gnome-dev-ethernet");//icon

	notify_notification_set_timeout(notification, 10*1000); //10秒

	error = NULL;
	success = notify_notification_show (notification, &error);

	if (!success) 
	{
		g_print ("That did not work ... \"%s\".\n", error->message);
		g_error_free (error);
	}

	notify_uninit ();
 
	return 0;
}


int main(int argc, char **argv)
{
    DBusConnection *connection;
	DBusError error;
    DBusMessage *message;
    DBusMessageIter iter;
    int reply_result = -1;
	
    dbus_error_init(&error);//将错误对象连接到dbus

    /* connect to the daemon bus */
	connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
	if(!connection)
	{
	    fprintf(stderr, "Failed to get a system DBus connection: %s\n", error.message);
	    exit(1);
	}

    dbus_bus_add_match(connection,
					   "type='signal',interface='com.kylin.MaintainMode.interface'",
					   &error );
    dbus_connection_flush(connection);

    if (dbus_error_is_set(&error))
	{
	    fprintf(stderr, "Match Error (%s)\n", error.message);
	    exit(1);
	}

    /*loop listening for signals being emmitted*/
    while(true)
	{
		dbus_connection_read_write(connection, 0);
		message= dbus_connection_pop_message(connection);//等待消息

        //没接收到等一秒钟继续
		if(NULL == message)
		{
			sleep(1);	
			continue;
		}

        //check if the message is the message we want to get
		if(dbus_message_is_signal(message,"com.kylin.MaintainMode.interface", "BootModeChanged"))
		{
		    printf("notifications is ought to be send\n");
			if(!dbus_message_iter_init(message, &iter))
				fprintf(stderr,"MessageHas no Param");

			else
			{
				/*try to get the content of signal*/
                dbus_message_iter_get_basic(&iter, &reply_result);
            	printf("Recetive Singal Value : %d\n", reply_result);
                if (reply_result == MM_OPEN_SUCESS)
                    notifications("运维模式开启成功\n需重启系统进入!");
                else if (reply_result == MM_OPEN_FAILED)
                    notifications("运维模式开启失败");
				else if (reply_result == MM_CLOSE_SUCESS)
                    notifications("运维模式关闭成功\n需重启系统退出!");
				else if (reply_result == MM_CLOSE_FAILED)
                    notifications("运维模式关闭失败");
			}
		}
	}
	//free the message
	dbus_message_unref(message);
	return 0;
}

