1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <windows.h>
#include <iostream>
#include <commctrl.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <time.h>
#include <Leaf.h>
#include <Vector>
#include "resource.h"
#include "globals.h"
#include "./include/misc.h"
#include "./include/Manager.h"
HINSTANCE hInst;
using namespace std;
int pythonFedId;
std::stringstream ss;
Leaf::Records* records;
/* This function refreshes the list box and adds the data logs from the database*/
void refreshListboxWithDataLogs(HWND hwndDlg, int listBoxId)
{
// If their are already records loaded then free them
if (records != NULL)
records->free();
HWND listbox = GetDlgItem(hwndDlg, listBoxId);
// Clear the listbox
SendMessage(listbox, LB_RESETCONTENT, 0 , 0);
records = db->getRecords("SELECT * FROM PYTHON_FEEDING WHERE `PYTHON_FED_ID` ORDER BY `FED_DATETIME` DESC");
if (records == NULL)
{
MessageBoxA(0, "Their was a problem trying to retrieve the pythons feeding", "Error", 0);
exit(1);
}
do
{
Leaf::Record* r = records->next();
if (r == NULL)
continue;
Leaf::Column* datetime = r->getColumn("FED_DATETIME");
long d = datetime->data.datal;
int listItem = (int)SendMessage(listbox, LB_ADDSTRING, 0,
(LPARAM) Misc::timeString("Fed: %d/%m/%Y", d).c_str());
SendMessage(listbox, LB_SETITEMDATA, listItem, (LPARAM) records->curIndex());
} while(records->moreRecordsAvailable());
}
BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
refreshListboxWithDataLogs(hwndDlg, LOGS_LISTBOX);
Misc::enableDlgItm(hwndDlg, DELETE_BTN, false);
}
return TRUE;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case MENU_NEW_ROYAL_PYTHON:
DialogBox(hInst, MAKEINTRESOURCE(NEW_PYTHON_DLG), NULL, (DLGPROC)DlgMain);
break;
case FED_TODAY_BTN:
Manager::snakeFed(30, time(NULL));
refreshListboxWithDataLogs(hwndDlg, LOGS_LISTBOX);
break;
case DELETE_BTN:
int a;
// a = MessageBoxA(0, (LPCSTR)"Are you sure you want to delete this log?", "", MB_YESNO);
// if (a == IDYES)
// {
cout << "Id: " << pythonFedId << endl;
ss.str("");
ss << "DELETE FROM `PYTHON_FEEDING` WHERE `PYTHON_FED_ID` = " << pythonFedId;
db->getRecords(ss.str().c_str());
refreshListboxWithDataLogs(hwndDlg, LOGS_LISTBOX);
pythonFedId = 0;
// Misc::enableDlgItm(hwndDlg, DELETE_BTN, false);
// }
break;
case LOGS_LISTBOX:
switch(HIWORD(wParam))
{
case LBN_SELCHANGE:
int lblItem, recordId;
Leaf::Record* selRecord;
Leaf::Column* pfid;
HWND listbox;
listbox = GetDlgItem(hwndDlg, LOGS_LISTBOX);
lblItem = (int)SendMessage(listbox, LB_GETCURSEL, 0, 0);
recordId = (int)SendMessage(listbox, LB_GETITEMDATA, lblItem, 0);
selRecord = records->getRecord(recordId);
pfid = selRecord->getColumn("PYTHON_FED_ID");
pythonFedId = pfid->data.datai;
Misc::enableDlgItm(hwndDlg, DELETE_BTN, true);
break;
}
break;
}
}
return TRUE;
}
return FALSE;
}
void setupDatabase()
{
bool error;
db = new Leaf::LeafDatabase();
// Open the database
if(!db->open("database.db"))
{
fprintf(stderr, "Can't open database %s", db->getErrMsg());
exit(1);
}
// Create tables
error = false;
if(!db->execute(
"CREATE TABLE IF NOT EXISTS `PYTHON_FEEDING`"
"("
"PYTHON_FED_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
"PYTHON_ID INTEGER NOT NULL,"
"FED_DATETIME DATETIME NOT NULL"
")"
))
{
MessageBoxA(0, "Problem creating table PYTHON_FEEDING", "Database problem", 0);
error = true;
}
if(!db->execute(
"CREATE TABLE IF NOT EXISTS `PYTHON_MANAGER_SETTINGS`"
"("
"PYTHON_MANAGER_SETTING_ID INTEGER PRIMARY KEY NOT NULL,"
"SETTING_VALUE NOT NULL"
")"
))
{
MessageBoxA(0, "Problem creating table PYTHON_MANAGER_SETTINGS", "Database problem", 0);
error = true;
}
if (error)
exit(1);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
setupDatabase();
if (strcmp(lpCmdLine,"wait") == 0)
{
Sleep(10000);
}
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}
|