[Wepg-devel] common HostUser.C,NONE,1.1 HostUser.h,NONE,1.1
Brought to you by:
leonvs
|
From: Leon v. S. <le...@us...> - 2004-08-03 17:13:06
|
Update of /cvsroot/wepg/common In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30230 Added Files: HostUser.C HostUser.h Log Message: ok --- NEW FILE: HostUser.C --- /* a pilot-link conduit for wepg Copyright (C) 2004 Leon van Stuivenberg <l.v...@ch...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "HostUser.h" #include "PalmAppInfo.h" #include "wepgutil.h" #include <string.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <pi-dlp.h> HostUser::HostUser( const char* user_id ) { m_userId = strdup( user_id ); m_newUserId = false; m_euserId = (char*)malloc( strlen( m_userId ) * 2 + 1); escapeString( getUserId(), m_euserId, '\'' ); } HostUser::~HostUser() { free( m_userId ); free( m_euserId ); } int HostUser::internalInit( MYSQL_RES *rs, MYSQL_ROW row ) { m_id = atol( row[ mysql_field_index( rs, "id" ) ] ); m_timezone = atol( row[ mysql_field_index( rs, "timezone" ) ] ); m_viewStart = atol( row[ mysql_field_index( rs, "view_start" ) ] ); m_dayEnd = atol( row[ mysql_field_index( rs, "day_end" ) ] ); m_loadAhead = atol( row[ mysql_field_index( rs, "load_ahead" ) ] ); return 0; } long HostUser::init( int psock, MYSQL *hostdb ) { int retval; const char* ufields = "id, name, user_id, timezone, view_start, day_end, load_ahead"; char sql[512]; char sql2[512]; MYSQL_RES *rs; MYSQL_ROW row; sprintf( sql, "SELECT %s FROM User WHERE user_id='%s'", ufields, m_euserId ); if ( mymysql_store( hostdb, sql, &rs ) ) { errprintf( "Query failed (%s)\n%s\n", mysql_error(hostdb), sql ); return 1; } if ( (row = mysql_fetch_row(rs)) == NULL ) { mysql_free_result( rs ); retry: struct PilotUser info; retval = dlp_ReadUserInfo( psock, &info ); if ( retval < 0 ) { errprintf( "ReadUserInfo failed, %i\n", retval ); return retval; } // for (unsigned j=0; j<sizeof(info); j++) printf("%c", ((char*)&info)[j] ); // printf("\n"); char euser_name[257]; escapeString( info.username, euser_name, '\'' ); sprintf( sql2, "SELECT %s FROM User " "WHERE (user_id is NULL OR user_id='') AND name='%s'", ufields, euser_name ); if ( mymysql_store( hostdb, sql2, &rs ) ) { errprintf( "Query failed (%s)\n%s\n", mysql_error(hostdb), sql2 ); return 1; } if ( (row = mysql_fetch_row(rs)) != NULL ) { long internal_user_id = atol( row[ mysql_field_index( rs, "id" ) ] ); mysql_free_result( rs ); sprintf( sql2, "UPDATE User SET user_id='%s' WHERE id=%i", m_euserId, (int)internal_user_id ); retval = mymysql_exec( hostdb, sql2 ); if ( retval ) return retval; if ( mymysql_store( hostdb, sql, &rs ) ) { errprintf( "Query failed (%s)\n%s\n", mysql_error(hostdb), sql ); return 1; } row = mysql_fetch_row(rs); m_newUserId = true; } else { printf("user:'%s'\n", info.username); errprintf( "unanounced user %s", (const char*)info.username ); mysql_free_result( rs ); sprintf( sql2, "INSERT INTO User (name,timezone,view_start,day_end,load_ahead) VALUES ('%s',%i,%i,%i,%i)", euser_name, 0, 1080, 120, 1); retval = mymysql_exec( hostdb, sql2 ); if ( retval ) return retval; goto retry; } } if ( row != NULL ) { retval = internalInit( rs, row ); mysql_free_result( rs ); if ( retval != 0 ) return retval; return 0; } errprintf( "no user record\n" ); mysql_free_result( rs ); return 1; } int HostUser::save( MYSQL *hostdb, PalmAppInfo& appinfo ) { char sql[ 512 ]; int retval; m_timezone = appinfo.getTimeZone(); setViewStartHour( appinfo.getPacked()->view_start_hour ); setViewStartMinute( appinfo.getPacked()->view_start_min ); setDayEndHour( appinfo.getPacked()->day_end_hour ); setDayEndMinute( appinfo.getPacked()->day_end_min ); m_loadAhead = appinfo.getPacked()->load_ahead; sprintf( sql, "UPDATE User SET timezone=%i, view_start=%i, day_end=%i, load_ahead=%i " "WHERE user_id='%s';", m_timezone, m_viewStart, m_dayEnd, m_loadAhead, (const char*)m_euserId ); retval = mymysql_exec( hostdb, sql ); if ( retval ) return retval; return 0; } --- NEW FILE: HostUser.h --- #ifndef __HostUser_H #define __HostUser_H #include <mysql.h> class HostUser { public: HostUser( const char* user_id ); ~HostUser(); long init( int psock, MYSQL *hostdb ); int save( MYSQL *hostdb, class PalmAppInfo& appinfo ); int getInternalId() const { return m_id; } const char* getUserId() const { return m_userId; } long getTimezone() const { return m_timezone; } int getLoadAhead() const { return m_loadAhead; } int getViewStartHour() { return m_viewStart / 60; } void setViewStartHour( int h ) { m_viewStart = (m_viewStart % 60) + (h * 60); } int getViewStartMinute() { return m_viewStart % 60; } void setViewStartMinute( int m ) { m_viewStart = (m_viewStart/60) * 60 + m; } int getDayEndHour() { return m_dayEnd / 60; } void setDayEndHour( int h ) { m_dayEnd = (m_dayEnd % 60) + (h * 60); } int getDayEndMinute() { return m_dayEnd % 60; } void setDayEndMinute( int m ) { m_dayEnd = (m_dayEnd/60) * 60 + m; } int getDayEndHourGMT() const { return getDayEndGMT() / 60; } int getDayEndMinuteGMT() const { return getDayEndGMT() % 60; } bool isNewUserId() const { return m_newUserId; } protected: char* m_userId; char* m_euserId; int m_id; int m_timezone; int m_viewStart; int m_dayEnd; int m_loadAhead; bool m_newUserId; private: int internalInit( MYSQL_RES *rs, MYSQL_ROW row ); int getDayEndGMT() const { int t = m_dayEnd-(m_timezone/60); while ( t < 0 ) t += 24*60; while ( t > 24*60) t-= 24*60; return t/60; } }; #endif |