unit UBalatarin;
interface
Uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Udownload, StdCtrls,Uabout,UUsers;
const
BALATARIN_VERSION1='Balabin 1.5';
BALATARIN_VERSION2='20 December 2007';
BALATARIN_HELP='http://persian.kamangir.net/?page_id=1170';
BALATARIN_HEADER='http://kamangir.net/files/balabin/header.htm';
BALATARIN_FOOTER='http://kamangir.net/files/balabin/footer.htm';
BALATARIN_INFO_URL='http://persian.kamangir.net/podpress_trac/web/820/1/Info.ini';
BALATARIN_INFO_FILENAME='Info.ini';
BALATARIN_RUN_INDICATOR='http://persian.kamangir.net/podpress_trac/web/1170/2/run.html';
BALATARIN_MULTIPLE=10;{Number of links to open subsequently after one click on the Multiple Button.}
BALATARIN_MAXLINKFETCH=25;
BALATARIN_MAXPAGE=5;
HTML_TEMP='temp.html';
HTML_DISP='display.html';
HTML_HEADER='header.html';
HTML_FOOTER='footer.html';
BMP_UPDATE='http://kamangir.net/files/balabin/update.gif';
BMP_REMOVE='http://kamangir.net/files/balabin/remove.gif';
BMP_SETTING='http://kamangir.net/files/balabin/settings.gif';
BMP_ABOUT='http://kamangir.net/files/balabin/about.gif';
BMP_MULTIPLE='http://kamangir.net/files/balabin/multiple.gif';
type
MBalatarin=class(Tobject)
download:Mdownload;
address:String;
tempdir:String;
datadir:string;
Users:Musers;
header,footer:TStringList;
constructor create;
destructor destroy; override;
function initialize:Boolean;
procedure Save;
procedure setaddress(s:string);
function temp(s:string):string;{Produces the address of a file in the temp directory}
procedure PLfromHTML(filename:string;links:TStringList); overload;
procedure PLfromHTML(links:TStringList); overload;
procedure Setting;
procedure fillup(form:TForm;jump:integer=0);{This is in fact a TFBSetting}
procedure GenerateReportOld;
procedure GenerateReport;{Now includes CSS}
procedure ReadPatches;
end;
var
Balatarin:MBalatarin;
implementation
uses Ulog,uini,UFBSetting,Umain;
{ MBalatarin }
constructor MBalatarin.create;
begin
inherited;
log.post('Engine started');
download:=Mdownload.create;
download.Online:=true;
log.post(' Download manager started');
Users:=MUsers.create;
log.post(' User structure created');
header:=TStringList.Create;
footer:=TStringList.Create;
end;
destructor MBalatarin.destroy;
begin
log.post('Engine closed off');
download.free;
users.Free;
header.free;
footer.free;
inherited;
end;
procedure MBalatarin.Setting;
var
form:TFBSetting;
begin
form:=TFBSetting.Create(Application);
form._Balatarin:=self;
fillup(form);
if Users.count>0 then form.GotoUser(1)
else
form.GotoUser(0);
form.ShowModal;
end;
function MBalatarin.temp(s: string): string;
begin
result:=tempdir+s;
end;
procedure MBalatarin.fillup(form: TForm;jump:integer=0);
var
i,j:integer;
forms:TFBSetting;
begin
forms:=TFBSetting(form);{Typecasting}
j:=forms.CUsers.ItemIndex;
forms.CUsers.Items.Clear;
for i:=1 to Users.count do
forms.CUsers.Items.Add(users.user(i).ID);
if j>Users.count-1 then j:=Users.count-1;
forms.GotoUser(j+1+jump);
end;
procedure MBalatarin.Save;
var
ini:Mini;
i:integer;
begin
log.post('Saving...');
ini:=Mini.create;
ini.Writestring('Balatarin','Address',address);
log.post('Saving list of users');
ini.Writeinteger('Users','Count',users.count);
for i:=1 to Users.count do
ini.Writestring('Users',format('ID%d',[i]),users.user(i).ID);
log.post('%d users to be saved',[Users.count]);
Users.Save(ini);
{Save window location and size}
ini.WriteInteger('Window','Left',fmain.Left);
ini.WriteInteger('Window','Top',fmain.Top);
ini.WriteInteger('Window','Width',fmain.Width);
ini.WriteInteger('Window','Height',fmain.Height);
ini.SaveTOFile(datadir+BALATARIN_INFO_FILENAME);
ini.Free;
log.post('Information saved.');
end;
procedure MBalatarin.GenerateReport;
var
s:Tstringlist;
i,count:integer;
begin
s:=TStringList.create;
s.add('');
s.add('');
s.add('
');
s.add(' ');
s.add(' '+BALATARIN_VERSION1+', '+BALATARIN_VERSION2+'');
s.add(' ');
s.add(' ');
s.add(' ');
s.add('');
s.add('');
s.add('');
s.add('');
s.add('');
s.add('
');
s.add('
');
s.add('
');
count:=0;
for i:=1 to Users.count do
if Users.user(i).Links.Count<>0 then
begin
count:=count+1;
Users.user(i).GenerateReport(s);
end;
if count>0 then
begin
s.add('
');
end
else
begin
s[s.Count-1]:='
No New Link
'
end;
s.add('
');
s.add('
');
for i:=1 to Users.count do
if Users.user(i).Links.Count=0 then Users.user(i).GenerateReport(s);
s.add('
');
{ s.add('
');}
s.AddStrings(footer);
s.add('
');
s.add('
');
s.add('');
s.add('');
s.SaveToFile(temp(HTML_DISP));
s.free;
end;
procedure MBalatarin.GenerateReportold;
var
s:Tstringlist;
i,count:integer;
begin
s:=TStringList.create;
s.add('');
s.add('');
s.add('');
s.add(' ');
s.add(' ');
s.add(' ');
s.add(' ');
s.add(' ');
s.add(' ');
s.add('');
s.add('');
{ s.add('
');}
s.AddStrings(header);
s.add('
');
s.add(format('You follow %d users, with %d non-visited links.',[Users.count,Users.NoLinks]));
s.add('
');
s.add(
'
'+
'
'+
'
'+
'
'+
'
');
s.add('
');
s.add('');
count:=0;
for i:=1 to Users.count do
if Users.user(i).Links.Count<>0 then
begin
count:=count+1;
Users.user(i).GenerateReport(s);
end;
if count>0 then
begin
s.add('
');
end
else
begin
s[s.Count-1]:='No New Link
'
end;
s.add('
');
s.add('');
for i:=1 to Users.count do
if Users.user(i).Links.Count=0 then Users.user(i).GenerateReport(s);
s.add('
');
{ s.add('
');}
s.AddStrings(footer);
s.add('');
s.add('');
s.SaveToFile(temp(HTML_DISP));
s.free;
end;
function MBalatarin.initialize:Boolean;
var
ini:Mini;
i,j:integer;
s:string;
begin
{Managing the temp directory}
Balatarin.download.execute(BALATARIN_RUN_INDICATOR,Balatarin.temp(HTML_TEMP));
tempdir:=ExtractFilePath(Application.ExeName)+'temp\';
log.post('Temp directory: "%s"',[tempdir]);
if not DirectoryExists(tempdir) then
begin
log.post('Temp directory does not exist.');
try
MkDir(tempdir);
log.post('Temp directory created.');
except
log.post('Temp directory cannot be created, halting.');
Application.Destroy;
end;
end;
{Managing the data directory}
datadir:=ExtractFilePath(Application.ExeName);
log.post('Data directory: "%s"',[datadir]);
Result:=false;
if not FileExists(datadir+BALATARIN_INFO_FILENAME) then{We should load it from the server}
if messagedlg('You do not seem to have a list of users, do you want me to download the list from the server?',mtconfirmation,[mbyes,mbno],mryes)=mryes then
if download.execute(BALATARIN_INFO_URL,BALATARIN_INFO_FILENAME)<>DOWNLOAD_DONE then showmessage('I cannot download it, sorry. You need to add the users yourself.');
if FileExists(datadir+BALATARIN_INFO_FILENAME) then
begin
Result:=true;
log.post('Setting file found');
ini:=Mini.create;
ini.Loadfromfile(datadir+BALATARIN_INFO_FILENAME);
s:=ini.readstring('Balatarin','Address','http://balatarin.com');
setaddress(s);
{Loading Users}
log.post('Loading list of users');
j:=ini.Readinteger('Users','Count',0);
for i:=1 to j do
users.Newuser.ID:=ini.Readstring('Users',format('ID%d',[i]),'');
log.post('%d users to be loaded',[users.count]);
Users.Load(ini);
{Loading window location and size}
log.post('Loading the Appearance');
fmain.Left:=ini.ReadInteger('Window','Left',fmain.Left);
fmain.Top:=ini.ReadInteger('Window','Top',fmain.Top);
fmain.Width:=ini.ReadInteger('Window','Width',fmain.Width);
fmain.Height:=ini.ReadInteger('Window','Height',fmain.Height);
ini.Free;
end
else
begin
s:=InputBox('Balatarin Address','Which balatarin domain do you use?','http://balatarin.com');
setaddress(s);
Fmain.Left:=round((Screen.Width-Fmain.Width)/2);
Fmain.top:=round((Screen.height-Fmain.height)/2);
log.post('Setting file not found');
end;
ReadPatches;
end;
procedure MBalatarin.PLfromHTML(links: TStringList);
begin
PLfromHTML(temp(HTML_TEMP),links);
end;
procedure MBalatarin.ReadPatches;
begin
{Loading the header and footer}
log.post('Reading the patches');
if download.execute(BALATARIN_HEADER,temp(HTML_HEADER))=DOWNLOAD_DONE then header.LoadFromFile(temp(HTML_HEADER));
if download.execute(BALATARIN_FOOTER,temp(HTML_FOOTER))=DOWNLOAD_DONE then footer.LoadFromFile(temp(HTML_FOOTER));
end;
procedure MBalatarin.PLfromHTML(filename:string;links:TStringList);
var
t:TextFile;
s:string;
index:integer;
begin
log.post('Starting parsing "%s"',[filename]);
{Checks}
{Does the file exist?}
if not FileExists(filename) then
begin
log.post(' File does not exist, parse failed!');
exit;
end;
{Stat parsing}
Assign(t,filename);
Reset(t);
links.Clear;
while not eof(t) do
begin
readln(t,s);
index:=Pos('href="/permlink',s);
if index<>0 then
begin
index:=index+7;
s:=Copy(s,index,length(s)-index+1);
index:=Pos('"',s);
s:=copy(s,1,index-1);
s:=address+s;
Links.Add(s);
log.post(' Found : %s',[s]);
end
end;
Close(t);
end;
procedure MBalatarin.setaddress(s: string);
begin
if s='' then address:=''
else
begin
if s[length(s)]<>'/' then s:=s+'/';
address:=s;
end;
end;
end.