GUIAgent.borg

GUIAgent.borg


`---------------------------------------
` FILE:        GUIAgent.borg
` AUTHOR:        Pieter Verheyden
` E-MAIL:        pverheyd@vub.ac.be
` LAST CHANGED:    16/12/2001
`---------------------------------------

    
{

load("DataObjects.borg");
load("../Libraries/PalmWidgets.borg");

` constant variables to hold unique id-numbers for each widget
WINDOW_1::101;
WINDOW_2::102;
WINDOW_3::103;
WINDOW_4::104;
WINDOW_5::105;
WINDOW_6::106;
WINDOW_7::107;
WINDOW_8::108;
MENUITEM_W1_1::201;
MENUITEM_W1_2::202;
MENUITEM_W1_3::203;
MENUITEM_W1_4::204;
MENUITEM_W1_5::205;
MENUITEM_W1_6::206;
MENUITEM_W1_7::207;
BUTTON_W1_1::301;
BUTTON_W1_2::302;
BUTTON_W2_1::303;
BUTTON_W2_2::304;
BUTTON_W3_1::305;
BUTTON_W3_2::306;
BUTTON_W4_1::307;
BUTTON_W4_2::308;
BUTTON_W5_1::309;
BUTTON_W5_2::310;
BUTTON_W6_1::311;
BUTTON_W6_2::312;
BUTTON_W6_3::313;
BUTTON_W7_1::314;
BUTTON_W7_2::315;
BUTTON_W8_1::316;
BUTTON_W8_2::317;
FIELD_W1_1::401;
FIELD_W2_1::402;
FIELD_W2_2::403;
FIELD_W2_3::404;
FIELD_W2_4::405;
FIELD_W2_5::406;
FIELD_W2_6::407;
FIELD_W2_7::408;
FIELD_W2_8::409;
FIELD_W3_1::410;
FIELD_W3_2::411;
FIELD_W3_3::412;
FIELD_W3_4::413;
FIELD_W4_1::414;
FIELD_W4_2::415;
FIELD_W4_3::416;
FIELD_W5_1::417;
FIELD_W6_1::418;
FIELD_W6_2::419;
FIELD_W7_1::420;
FIELD_W7_2::421;
FIELD_W7_3::422;
FIELD_W8_1::423;
FIELD_W8_2::424;
FIELD_W8_3::425;
CHECKBOX_W4_1::501;


` variable to hold a reference to the MPAgent
mpagent_: void;
` variable to hold the date of which the meetings must be shown
currentdate_: date();


` setup of communication with the MPAgent
initMPAgent():: {
    mpagentnamestring:fieldGetText(FIELD_W6_1);
    mpagent_:=agent(mpagentnamestring);
    password:fieldGetText(FIELD_W6_2);
    mpagent_->setupGUIAgent(agentname, password)
};

initMPAgentSuccess():: 
    freeWindow(WINDOW_6);


` get the schedule and show it
getDateSchedule():: {
    fieldSetText(FIELD_W1_1, "Please wait ...");
    mpagent_->dateSchedule(currentdate_)
};    

retrieveDateSchedule(schedulestring)::
    fieldSetText(FIELD_W1_1, schedulestring);


` dialog messages to the user
showNotification(msg)::
    showDialog(0, msg);

showError(msg)::
    showDialog(3, msg);    


` associate each button and menu item with an action/function
buttonFunction(id):: {
          if(((id=BUTTON_W1_2)|(id=MENUITEM_W1_7)),
        {
        mpagent_->resetGUIAgent();
        freeWindow(WINDOW_1);
        uiCleanUp()
              },
    if(id=BUTTON_W6_2,
        {
        freeWindow(WINDOW_6);
        uiCleanUp()
              },
    if(id=BUTTON_W6_3, initCreateMPAgentWindow(),
    if(id=BUTTON_W7_1, createMPAgent(),
    if(id=BUTTON_W7_2, freeWindow(WINDOW_7),
    if(id=BUTTON_W6_1, initMPAgent(),
    if(((id=BUTTON_W1_1)|(id=MENUITEM_W1_5)),getDateSchedule(), 
    if(id=BUTTON_W2_1, setupMyMeeting(),
    if(id=BUTTON_W2_2, freeWindow(WINDOW_2),
    if(id=BUTTON_W3_1, changePlace(),
    if(id=BUTTON_W3_2, freeWindow(WINDOW_3),
    if(id=BUTTON_W4_1, cancelMeeting(),
    if(id=BUTTON_W4_2, freeWindow(WINDOW_4),
    if(id=BUTTON_W5_1, changeCurrentDate(),
    if(id=BUTTON_W5_2, freeWindow(WINDOW_5),
    if(id=BUTTON_W8_1, changePassword(),
    if(id=BUTTON_W8_2, freeWindow(WINDOW_8))
    ))))))))))))))))
};

menuItemFunction(id):: {
    if(id=MENUITEM_W1_1, initSetupMeetingWindow(),
    if(id=MENUITEM_W1_2, initEditMeetingWindow(),
    if(id=MENUITEM_W1_3, initCancelMeetingWindow(),
    if(id=MENUITEM_W1_4, initChangeDateWindow(),
    if(id=MENUITEM_W1_5, buttonFunction(id),
    if(id=MENUITEM_W1_6, initChangePasswordWindow(),
    if(id=MENUITEM_W1_7, buttonFunction(id))
    ))))))
};


` make and place the main window
initMainWindow():: {                
    newWindow(WINDOW_1, "MeetingPlanner v1.0b - <" + text(mpagent_) + ">");
    
    menuAddItem(MENUITEM_W1_1, 0, "Setup Meeting", "s", "menuItemFunction");
    menuAddItem(MENUITEM_W1_2, MENUITEM_W1_1, "Edit Meeting", "e", "menuItemFunction");
    menuAddItem(MENUITEM_W1_3, MENUITEM_W1_2, "Cancel Meeting", "c", "menuItemFunction");
    menuAddItem(MENUITEM_W1_4, MENUITEM_W1_3, "Change Date", "v", "menuItemFunction");
    menuAddItem(MENUITEM_W1_5, MENUITEM_W1_4, "Refresh Schedule", "r", "menuItemFunction");
    menuAddItem(MENUITEM_W1_6, MENUITEM_W1_5, "Change Password", "p", "menuItemFunction");
    menuAddItem(MENUITEM_W1_7, MENUITEM_W1_6, "Exit", "w", "menuItemFunction");
    
    newButton(BUTTON_W1_1, "Refresh", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W1_1, 5, 145);
    
    newButton(BUTTON_W1_2, "Exit", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W1_2, 115, 145);
    
    newField(FIELD_W1_1, "", 150, 125, 999, 0, 0, 0, 0, 1, 0);
    widgetPlace(FIELD_W1_1, 5, 15);
    
    showWindow(WINDOW_1)
};

` make and place the "Setup Meeting" window
initSetupMeetingWindow():: {
    newWindow(WINDOW_2, "MeetingPlanner: Setup Meeting");

    newField(FIELD_W2_1, "Date", 20, 10, 10, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W2_1, 5, 10);

    newField(FIELD_W2_2, "From", 20, 10, 5, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W2_2, 55, 10);

    newField(FIELD_W2_3, "Till", 20, 10, 5, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W2_3, 105, 10);

    newField(FIELD_W2_4, "Subject", 150, 10, 250, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W2_4, 5, 35);

    newField(FIELD_W2_5, "Place", 150, 10, 50, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W2_5, 5, 60);

    newField(FIELD_W2_6, "Guests (use ; to distinguish them, no empty spaces allowed)", 150, 10, 999, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W2_6, 5, 85);

    newField(FIELD_W2_7, "%", 20, 10, 3, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W2_7, 5, 110);

    newButton(BUTTON_W2_1, "OK", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W2_1, 70, 145);

    newButton(BUTTON_W2_2, "Cancel", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W2_2, 115, 145);

    showWindow(WINDOW_2)
};

` check whether a field is filled with text
` if the field is not filled, an error dialog is shown to the user and false is returned
checkFieldFilled(fieldtext, fieldstring):: {
    if(fieldtext="",
        {
        showError("No value found for " + fieldstring);
        false
        },
        true
    )
};

` parse functions

` input: "day/month/year"
` output: [day,month,year]
` if parsing fails, an error dialog is shown to the user and false is returned
parseDateText(datetext):: {
    indexday: strstr(datetext, "/");
    daystring: substr(datetext,if(indexday=3, indexday-2, indexday-1),indexday-1);
    reststring: substr(datetext,indexday+1,length(datetext));
    daynumber: number(daystring);
    
    indexmonth: strstr(reststring, "/");
    monthstring: substr(reststring,if(indexmonth=3, indexmonth-2, indexmonth-1),indexmonth-1);
    reststring:= substr(reststring,indexmonth+1,length(reststring)); 
    monthnumber: number(monthstring);  

    yearnumber: number(reststring);                    

    if(((!is_void(daynumber)) & (!is_void(monthnumber)) & (!is_void(yearnumber))),
        [daynumber,monthnumber,yearnumber],
        {
        showError("You entered a wrong date");
        false
        }
    )
};

` input: "hour.minutes"
` output: [hour,minutes]
` if parsing fails, an error dialog is shown to the user and false is returned
parseTimeText(timetext, fieldtext):: {
    indexhour: strstr(timetext, ".");
    hourstring: substr(timetext,if(indexhour=3, indexhour-2, indexhour-1),indexhour-1);
    reststring: substr(timetext,indexhour+1,length(timetext));
    hournumber: number(hourstring);

    minutesnumber: number(reststring);

    if(((!is_void(hournumber)) & (!is_void(minutesnumber))),
        [hournumber,minutesnumber],
        {
        showError("You entered a wrong time " + fieldText);
        false
        }
    )
};

` input: "agentname1, agentname2, ..."
` ouput: [, , ...]
parseGuestsText(gueststext):: {
    guesttable_: makeVoidTable(8);
    reststring: gueststext;
    while((strstr(reststring, ";")!=0),
        {
        guestindex: strstr(reststring, ";");
        gueststring: substr(reststring,0,guestindex-1);
        reststring:= substr(reststring,guestindex+1,length(reststring)); 
        guesttable_.addElement(agent(gueststring))
        }
      );
    guesttable_.addElement(agent(reststring));
    guesttable_.deleteAllVoids();
    guesttable_.returnVoidTable()
};

` setup the meeting
setupMyMeeting():: {
    datetext: fieldGetText(FIELD_W2_1);
    begintimetext: fieldGetText(FIELD_W2_2);
    endtimetext: fieldGetText(FIELD_W2_3);
    subjecttext:fieldGetText(FIELD_W2_4); 
    placetext:fieldGetText(FIELD_W2_5); 
    gueststext:fieldGetText(FIELD_W2_6);  
    percentagetext:fieldGetText(FIELD_W2_7);

    if(((checkFieldFilled(datetext, "Date")) & (checkFieldFilled(begintimetext, "From")) & (checkFieldFilled(begintimetext, "Till")) 
        & (checkFieldFilled(subjecttext, "Subject")) & (checkFieldFilled(placetext, "Place"))& (checkFieldFilled(gueststext, "Guests"))
        & (checkFieldFilled(percentagetext, "%"))),
        {
        thedate: parseDateText(datetext);
        begintime: parseTimeText(begintimetext, "From");
        endtime: parseTimeText(endtimetext, "Till");
        if(((!is_false(thedate)) & (!is_false(begintime)) & (!is_false(endtime))),
            {
            datetable: [thedate[1],thedate[2],thedate[3],begintime,endtime];
            guesttable: parseGuestsText(gueststext);
            percentage: number(percentagetext);
            mpagent_->setupMeeting(datetable, guesttable, percentage, subjecttext, agent(placetext))
            }
        )        
        }
    )    
};          
    
` when the setup of a meeting is finished, the "Setup Meeting" window must close
setupMeetingFinished()::
    freeWindow(WINDOW_2);    


` make and place the "Edit Meeting" window
` only the place of a meeting can be changed
initEditMeetingWindow():: {
    newWindow(WINDOW_3, "MeetingPlanner: Edit Meeting");

    newField(FIELD_W3_1, "Date", 20, 10, 10, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W3_1, 5, 10);

    newField(FIELD_W3_2, "From", 20, 10, 5, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W3_2, 55, 10);

    newField(FIELD_W3_3, "Till", 20, 10, 5, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W3_3, 105, 10);

    newField(FIELD_W3_4, "New place", 150, 10, 50, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W3_4, 5, 35);

    newButton(BUTTON_W3_1, "OK", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W3_1, 70, 145);

    newButton(BUTTON_W3_2, "Cancel", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W3_2, 115, 145);

    showWindow(WINDOW_3)
};

` change the place (as a caller of a meeting)
changePlace():: {
    datetext: fieldGetText(FIELD_W3_1);
    begintimetext: fieldGetText(FIELD_W3_2);
    endtimetext: fieldGetText(FIELD_W3_3);
    newplacetext:fieldGetText(FIELD_W3_4); 
    
    if(((checkFieldFilled(datetext, "Date")) & (checkFieldFilled(begintimetext, "From")) & (checkFieldFilled(begintimetext, "Till")) 
        & (checkFieldFilled(newplacetext, "New Place"))),
        {
        thedate: parseDateText(datetext);
        begintime: parseTimeText(begintimetext, "From");
        endtime: parseTimeText(endtimetext, "Till");
        if(((date!~false) & (begintime!~false) & (endtime!~false)),
            {
            datetable: [thedate[1],thedate[2],thedate[3],begintime,endtime];
            mpagent_->newPlace(datetable, agent(newplacetext))
            }
        )        
        }
    )    
};

` newPlace request finished
newPlaceFinished()::
     freeWindow(WINDOW_3);    
                  

` make and place the "Cancel Meeting" window
initCancelMeetingWindow():: {
    newWindow(WINDOW_4, "MeetingPlanner: Cancel Meeting");

    newField(FIELD_W4_1, "Date", 20, 10, 10, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W4_1, 5, 10);

    newField(FIELD_W4_2, "From", 20, 10, 5, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W4_2, 55, 10);

    newField(FIELD_W4_3, "Till", 20, 10, 5, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W4_3, 105, 10);

    newCheckBox(CHECKBOX_W4_1, "I'm the caller of this meeting", 100, 10, "");
    widgetPlace(CHECKBOX_W4_1, 5, 40);

    newButton(BUTTON_W4_1, "OK", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W4_1, 70, 145);

    newButton(BUTTON_W4_2, "Cancel", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W4_2, 115, 145);

    showWindow(WINDOW_4)
};

` cancel a meeting
cancelMeeting():: {
    datetext: fieldGetText(FIELD_W4_1);
    begintimetext: fieldGetText(FIELD_W4_2);
    endtimetext: fieldGetText(FIELD_W4_3);
    
    if(((checkFieldFilled(datetext, "Date")) & (checkFieldFilled(begintimetext, "From")) & (checkFieldFilled(begintimetext, "Till"))),
        {
        thedate: parseDateText(datetext);
        begintime: parseTimeText(begintimetext, "From");
        endtime: parseTimeText(endtimetext, "Till");
        if(((date!~false) & (begintime!~false) & (endtime!~false)),
            {
            datetable: [thedate[1],thedate[2],thedate[3],begintime,endtime];
            if(checkBoxGetValue(CHECKBOX_W4_1)=1,
                mpagent_->cancelMyMeeting(datetable),
                mpagent_->cancelGuestMeeting(datetable)
            )
            }
        )        
        }
    )    
};

` cancel finished
cancelFinished():: 
    freeWindow(WINDOW_4);
    
` make and place the "Change Date" window
initChangeDateWindow():: {
    newWindow(WINDOW_5, "MeetingPlanner: Change Date");

    newField(FIELD_W5_1, "Date", 20, 10, 10, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W5_1, 5, 10);

    newButton(BUTTON_W5_1, "OK", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W5_1, 70, 145);

    newButton(BUTTON_W5_2, "Cancel", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W5_2, 115, 145);

    showWindow(WINDOW_5)
};

` change the current date
changeCurrentDate():: {
    datetext: fieldGetText(FIELD_W5_1);
    
    if(checkFieldFilled(datetext, "Date"),
        {
        thedate: parseDateText(datetext);
        if(date!~false,
            {
            currentdate_:=thedate;
            getDateSchedule();
            freeWindow(WINDOW_5)
            }
        )        
        }
    )    
};
    

` make and place the "Identification" window
` this is the very first window (before the main window appears)
initIdentificationWindow():: {
    newWindow(WINDOW_6, "MeetingPlanner: Identification");
    
    newField(FIELD_W6_1, "MPAgent", 150, 10, 50, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W6_1, 5, 30);    

    newField(FIELD_W6_2, "Password", 150, 10, 25, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W6_2, 5, 50);    

    newButton(BUTTON_W6_3, "Create MPAgent", 60, 10, "buttonFunction");
    widgetPlace(BUTTON_W6_3, 5, 145);

    newButton(BUTTON_W6_1, "OK", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W6_1, 70, 145);

    newButton(BUTTON_W6_2, "Cancel", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W6_2, 115, 145);

    showWindow(WINDOW_6)
};

` make and place the "Create MPAgent" window
initCreateMPAgentWindow():: {
    newWindow(WINDOW_7, "MeetingPlanner: Create MPAgent");

    newField(FIELD_W7_1, "New MPAgent", 150, 10, 50, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W7_1, 5, 30);    

    newField(FIELD_W7_2, "Password", 150, 10, 25, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W7_2, 5, 50);    

    newField(FIELD_W7_3, "Re-enter password", 150, 10, 25, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W7_3, 5, 70);

    newButton(BUTTON_W7_1, "OK", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W7_1, 70, 145);

    newButton(BUTTON_W7_2, "Cancel", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W7_2, 115, 145);

    showWindow(WINDOW_7)
};

` create a new MPAgent
createMPAgent():: {
    mpagentstring:fieldGetText(FIELD_W7_1);
    password1:fieldGetText(FIELD_W7_2); 
    password2:fieldGetText(FIELD_W7_3);

    if(((checkFieldFilled(mpagentstring, "New MPAgent")) & (checkFieldFilled(password1, "Password")) & (checkFieldFilled(password2, "Re-enter password"))),
        if(password1=password2,
            {
            mpagent:new(mpagentstring, load("MPAgent.borg"));
            mpagent->setPassword(password1);
            freeWindow(WINDOW_7)    
            },
            showDialog(3, "Password mismatch")
        )
    ) 
};

` make and place the "Change password" window 
initChangePasswordWindow():: {
    newWindow(WINDOW_8, "MeetingPlanner: Change Password");

    newField(FIELD_W8_1, "Current password", 150, 10, 25, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W8_1, 5, 30);    

    newField(FIELD_W8_2, "New password", 150, 10, 25, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W8_2, 5, 50);    

    newField(FIELD_W8_3, "Re-enter new password", 150, 10, 25, 1, 0, 1, 0, 0, 0);
    widgetPlace(FIELD_W8_3, 5, 70);

    newButton(BUTTON_W8_1, "OK", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W8_1, 70, 145);

    newButton(BUTTON_W8_2, "Cancel", 40, 10, "buttonFunction");
    widgetPlace(BUTTON_W8_2, 115, 145);

    showWindow(WINDOW_8)
};

` set a new password
changePassword():: {
    cpassword:fieldGetText(FIELD_W8_1);
    password1:fieldGetText(FIELD_W8_2);
    password2:fieldGetText(FIELD_W8_3);

    if(((checkFieldFilled(cpassword, "Current password")) & (checkFieldFilled(password1, "New password")) & (checkFieldFilled(password2, "Re-enter new password"))),
        if(password1=password2,
            mpagent_->newPassword(cpassword, password1),    
            showDialog(3, "Password mismatch")
        )
    ) 
};

newPasswordSuccess()::
    freeWindow(WINDOW_8);
        
    
initIdentificationWindow()

}