cNotify

/*
 * cNotify.h
 *
 *  Created on: Jun 16, 2014
 *      Author: chuck
 */

#ifndef CNOTIFY_H_
#define CNOTIFY_H_

/*
 * Graphical Skeleton Program 2-18-2012
 * Use at your own risk
 */

#include 

/* sessions */
PGconn *dbConn;
int clientID;
char *dbIP;
char *n_val;
void setClientID();

/**** Widget Creation ****/
Widget createMenuBar(Widget appManager);
void createMenuBarItems(Widget menuBar, String menuName);
Widget createAppContent(Widget appManager, Widget menuBar, Widget statusBar);
Widget createStatusBar(Widget appManager);
void createMsgBox(String title, String msg);

/**** Widget Callbacks ****/
void menuBarCB(Widget widget, XtPointer clientData, XtPointer callData);
Boolean notifyCB(XtPointer appShell);
void msgBoxCB(Widget widget, XtPointer clientData, XtPointer callData);

/**** Sets Status Bar Text ****/
void showStatus(String status);

/**** Gets Menu Item Accelerator For menuBarCB ****/
KeySym getMnemonic(String menuName, String menuItemName);


#endif /* CNOTIFY_H_ */

/*
 ============================================================================
 Name        : cNotify.c
 Author      : chuck
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include 
#include 
#include 

#include "cNotify.h"

/* connect and get db sequence soe, create a file for dialogs */
void setClientID() {

	const char *connInfo;
	PGresult   *res;
	//dbIP = "192.168.1.100";
	dbIP = "192.168.1.120";

	/* connect to cnog db */
	asprintf(&connInfo,
		"hostaddr=%s port=%s dbname=%s user=%s",
			dbIP, "5432", "psqlbsd", "psqlbsd");
	printf("connInfo = %s\n", connInfo);
	dbConn = PQconnectdb(connInfo);

	/* Check to see that the backend connection was successfully made */
	if(PQstatus(dbConn) != CONNECTION_OK) {
		fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(dbConn));
	}

	/* get, set clientID */
	res = PQexec(dbConn, "select nextval('soe_id_seq');");
	printf("PQexec!\n");

	printf("PQgetvalue is %s!\n", PQgetvalue(res, 0, 0));

	char **ptr = NULL;
	//clientID = strtol(PQgetvalue(res, 0, 0), ptr, 10);
	//printf("clientID = %d\n", clientID);
	PQclear(res);

	/* listen - why i'm here */
	PQexec(dbConn, "listen noe;");

}

/**** public ****/
Widget statusTextField;
Widget contentManager;

/**** creates the menu bar then creates each menu item ****/
Widget createMenuBar(Widget appManager) {

	Widget menuBar;

	XmString file;
	XmString edit;

	file = XmStringCreateLocalized ("File");
	edit = XmStringCreateLocalized ("Edit");

	menuBar = XmVaCreateSimpleMenuBar(appManager, "menuBar",
		XmVaCASCADEBUTTON, file, 'F',
		XmVaCASCADEBUTTON, edit, 'E',
		XmNleftAttachment, XmATTACH_FORM,
		XmNrightAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		NULL);

	XmStringFree(file);
	XmStringFree(edit);

	createMenuBarItems(menuBar, "File");
	createMenuBarItems(menuBar, "Edit");

	return menuBar;
}

/**** creates the menu items ****/
void createMenuBarItems(Widget menuBar, String menuName) {

	if(!strcmp(menuName, "File")) {

		XmString open;
		XmString save;
		XmString exit;
		XmString exitAccel;

		open = XmStringCreateLocalized("Open...");
		save = XmStringCreateLocalized("Save...");
		exit = XmStringCreateLocalized("Exit");
		exitAccel = XmStringCreateLocalized("Ctrl+Q");

		XmVaCreateSimplePulldownMenu (menuBar, "fileMenu", 0, (XtCallbackProc)menuBarCB,
			XmVaPUSHBUTTON, open, 'O', NULL, NULL,
			XmVaPUSHBUTTON, save, 'S', NULL, NULL,
			XmVaSEPARATOR,
			XmVaPUSHBUTTON, exit, 'x', "CtrlQ", exitAccel,
			NULL);

		XmStringFree(open);
		XmStringFree(save);
		XmStringFree(exit);

		XmStringFree(exitAccel);

		return;
	}

	if(!strcmp(menuName, "Edit")) {

		XmString cut;
		XmString copy;
		XmString paste;
		XmString clear;

		cut = XmStringCreateLocalized("Cut");
		copy = XmStringCreateLocalized("Copy");
		paste = XmStringCreateLocalized("Paste");
		clear = XmStringCreateLocalized("Clear");

		XmVaCreateSimplePulldownMenu (menuBar, "editMenu", 1, (XtCallbackProc)menuBarCB,
			XmVaPUSHBUTTON, cut, 't', NULL, NULL,
			XmVaPUSHBUTTON, copy, 'C', NULL, NULL,
			XmVaPUSHBUTTON, paste, 'P', NULL, NULL,
			XmVaSEPARATOR,
			XmVaPUSHBUTTON, clear, 'l', NULL, NULL,
			NULL);

		XmStringFree(cut);
		XmStringFree(copy);
		XmStringFree(paste);
		XmStringFree(clear);
	}

	return;
}

/**** substitute here with a form or something ****/
Widget createAppContent(Widget appManager, Widget menuBar, Widget statusBar) {

	Widget appContent;

	/* place holder scrolled window */
	appContent = XtVaCreateWidget("appContent",
		xmScrolledWindowWidgetClass, appManager,
		XmNwidth, 400,
		XmNheight, 400,
		XmNscrollingPolicy, XmAUTOMATIC,
		XmNtopAttachment, XmATTACH_WIDGET,
		XmNtopWidget, menuBar,
		XmNleftAttachment, XmATTACH_FORM,
		XmNrightAttachment, XmATTACH_FORM,
		XmNbottomAttachment, XmATTACH_WIDGET,
		XmNbottomWidget, statusBar,
		NULL);

	contentManager = appContent;

	return appContent;
}

/**** to tell the user what's happening ****/
Widget createStatusBar(Widget appManager) {

	Widget statusBar;

	statusBar = XtVaCreateManagedWidget ("statusBar",
		xmTextFieldWidgetClass, appManager,
		XmNeditable, False,
		XmNcursorPositionVisible, False,
		XmNshadowThickness, 0,
		XmNleftAttachment, XmATTACH_FORM,
		XmNrightAttachment, XmATTACH_FORM,
		XmNbottomAttachment, XmATTACH_FORM,
		NULL);

	statusTextField = statusBar;

	return statusBar;
}

/**** a simple (yeah right) message box ****/
void createMsgBox(String title, String msg) {

	Widget dialog;
	Arg args[14];
	int n = 0;

	XmString xmTitle = XmStringCreateLocalized(title);
	XmString xmMsg = XmStringCreateLocalized(msg);

	XtSetArg(args[n], XmNdialogTitle, xmTitle); n++;
	XtSetArg(args[n], XmNmessageString, xmMsg); n++;

	dialog = XmCreateMessageDialog(contentManager, "simple", args, n);

	XtManageChild(dialog);

	XtAddCallback(dialog, XmNokCallback, msgBoxCB, (XtPointer)"ok");
	XtAddCallback(dialog, XmNcancelCallback, msgBoxCB, (XtPointer)"cancel");
	XtAddCallback(dialog, XmNhelpCallback, msgBoxCB, (XtPointer)"help");

	XmStringFree(xmTitle);
	XmStringFree(xmMsg);

	return;
}

/**** menuBar handler ****/
void menuBarCB(Widget widget, XtPointer clientData, XtPointer callData) {

        String menuName = XtName(XtParent(widget));
	String menuItemName = XtName(widget);
        KeySym mnemonic = getMnemonic(menuName, menuItemName);

	if(!strcmp(menuName, "fileMenu")) {
                switch(mnemonic) {
                        case 'O':
                                /* printf("you clicked Open\n"); */
				showStatus("Open");
				createMsgBox("Open", "Put open code here");
                                break;
                        case 'S':
                                /* printf("you clicked Save\n"); */
				showStatus("Save");
				createMsgBox("Save", "Put save code here");
                                break;
                        case 'x':
                                /* printf("you clicked Exit\n"); */
                                /* showStatus("Exit"); */
                                /* createMsgBox("Exit", "Put exit code here"); */
                                exit(0);

                                break;

                        default:
                                printf("pg_menu.php\n");
                                return;
                }
        }

        if(!strcmp(menuName, "editMenu")) {
                switch(mnemonic) {
                        case 't':
                                /* printf("you clicked Cut\n"); */
				showStatus("Cut");
				createMsgBox("Cut", "Put cut code here");
                                break;
                        case 'C':
                                /* printf("you clicked Copy\n"); */
				showStatus("Copy");
				createMsgBox("Copy", "Put copy code here");

                                break;
                        case 'P':
                                /* printf("you clicked Paste\n"); */
				showStatus("Paste");
				createMsgBox("Paste", "Put paste code here");
                                break;
                        case 'l':
                                /* printf("you clicked Clear\n"); */
				showStatus("Clear");
				createMsgBox("Clear", "Put clear code here");
                                break;
                        default:
                                printf("pg_menu.php\n");
                                return;
                }
        }

        return;
}

/**** check for notifications ****/
Boolean notifyCB(XtPointer appShell) {

	PGnotify   *notify;
	char *sql;
	PGresult *res;

	PQconsumeInput(dbConn);

	if((notify = PQnotifies(dbConn)) != NULL) {
		if(!strcmp(notify->relname, "noe")) {
			printf("notified\n");
			asprintf(&sql,
				"select n_val from noe where s_id = %d order by n_id desc;", clientID);
			printf("%s\n", sql);
			res = PQexec(dbConn, sql);
			if(PQntuples(res) > 0) {
				printf("more than zero\n");
				n_val = PQgetvalue(res, 0, 0);
				printf("n_val = %s\n", n_val);
			}
			PQclear(res);
		}
	}

	/**** keep checking ****/
	return False;
}

/**** simple menu item dialog handler ****/
void msgBoxCB(Widget widget, XtPointer clientData, XtPointer callData) {

	showStatus("");

	return;
}

/**** what's happening? ****/
void showStatus(String status) {

	XmTextSetString(statusTextField, status);

	return;
}

/**** this is a substitute for an array or something ****/
KeySym getMnemonic(String menuName, String menuItemName) {

        KeySym mnemonic;

	if(!strcmp(menuName, "fileMenu")) {
		if(!strcmp(menuItemName, "button_0")) return XStringToKeysym("O");
		if(!strcmp(menuItemName, "button_1")) return XStringToKeysym("S");
		if(!strcmp(menuItemName, "button_2")) return XStringToKeysym("x");
	}

	if(!strcmp(menuName, "editMenu")) {
		if(!strcmp(menuItemName, "button_0")) return XStringToKeysym("t");
		if(!strcmp(menuItemName, "button_1")) return XStringToKeysym("C");
		if(!strcmp(menuItemName, "button_2")) return XStringToKeysym("P");
		if(!strcmp(menuItemName, "button_3")) return XStringToKeysym("l");
	}

	return NULL;
}

/**** the main program ****/
main(int argc, char *argv[]) {

	Widget appShell;
	Widget appManager;
	Widget appContent;

	Widget menuBar;
	Widget statusBar;

	XtAppContext appContext;

	setClientID();

	appShell = XtVaOpenApplication(&appContext, "appClass",
		NULL, 0, &argc, argv, NULL, applicationShellWidgetClass,
		NULL);

	appManager = XtVaCreateManagedWidget("appManager",
		xmFormWidgetClass, appShell,
		NULL);

	menuBar = (Widget)createMenuBar(appManager);
	XtManageChild(menuBar);

	statusBar = createStatusBar(appManager);
	XtManageChild(statusBar);

	appContent = createAppContent(appManager, menuBar, statusBar);
	XtManageChild(appContent);

	XtRealizeWidget(appShell);

	XtAppAddWorkProc(appContext, notifyCB, appShell);

	XtAppMainLoop(appContext);

}