Index: cprs/branches/tmg-cprs/CPRS-Chart/fImages.pas
===================================================================
--- cprs/branches/tmg-cprs/CPRS-Chart/fImages.pas	(revision 800)
+++ cprs/branches/tmg-cprs/CPRS-Chart/fImages.pas	(revision 801)
@@ -41,4 +41,6 @@
     published
   end;
+
+  TImgTransferMethod = (itmDropbox,itmDirect,itmRPC);
 
   TfrmImages = class(TfrmPage)
@@ -97,6 +99,6 @@
     ImageInfoList : TList;
     LastDisplayedTIUIEN : AnsiString;
-    CurImageToLoad : integer;
-    InTimerFn : Boolean;
+    NumImagesAvailableOnServer : integer;
+    ImageIndexLastDownloaded : integer;
     procedure EnsureImageListLoaded();
     procedure ClearImageList();
@@ -108,9 +110,13 @@
     procedure UpdateImageInfoMemo(Rec: TImageInfo);
     function FileSize(fileName : wideString) : Int64;
+    function GetImagesCount : integer;
+    function GetImageInfo(Index : integer) : TImageInfo;
+    procedure SetupTimer;
   public
     CacheDir : AnsiString;
-    UseDropBox : boolean;
+    TransferMethod : TImgTransferMethod;
     DropBoxDir : string;
     NullImageName : AnsiString;
+    DownloadImagesInBackground : boolean;
     function Decode(input: AnsiString) : AnsiString;
     function Encode(input: AnsiString) : AnsiString;
@@ -127,6 +133,25 @@
     function DecodeBarcode(LocalFNamePath,ImageType: AnsiString): AnsiString;
     procedure EnsureImagesDownloaded(ImagesList : TStringList);
+    procedure EnsureALLImagesDownloaded;
+    property ImagesCount : integer read GetImagesCount;
+    property ImageInfo[index : integer] : TImageInfo read GetImageInfo;
+    procedure GetThumbnailBitmapForFName (FName : string; Bitmap : TBitmap);
+    function ThumbnailIndexForFName (FName : string) : integer;
   published
   end;
+
+Const
+  IMAGE_TRANSFER_METHODS : Array[itmDropbox..itmRPC] of string[32] = (
+    'Dropbox Transfer', 'Direct Access', 'Embedded in RPCs');
+  IMAGE_DOWNLOAD_DELAY_BACKGROUND = 30000;
+  IMAGE_DOWNLOAD_DELAY_FOREGROUND = 100;
+  NOT_YET_CHECKED_SERVER = -2;
+
+  //NOTE: If order is changed in ThumbsImageList, these numbers should be changed
+  IMAGE_INDEX_IMAGE = 0;
+  IMAGE_INDEX_ADOBE  = 1;
+  IMAGE_INDEX_VIDEO  = 2;
+  IMAGE_INDEX_SOUND  = 3;
+  IMAGE_INDEX_MISC   = 4;
 
 var
@@ -157,34 +182,26 @@
 begin
   inherited;
-  if InTimerFn=false then begin
-    InTimerFn := true;
-    EnsureImageListLoaded(); //only does RPC call if CurImageToLoad = 0
-    if CurImageToLoad <> -1 then begin //-1 means RPC-> no avail images.
-      if CurImageToLoad < ImageInfoList.Count then begin
-        ImageTransferForm.ProgressMsg.Caption := 'Downloading Images';
-        //if ImageTransferForm.Visible = false then begin
-        //   ImageTransferForm.Show;
-        //end;
-        DownloadToCache(CurImageToLoad); //Only load 1 image per timer firing.
-        SetupTab(CurImageToLoad);
-        Inc(CurImageToLoad);
-        if TabControl.TabIndex < 0 then TabControl.TabIndex := 0;
-        TabControlChange(self);
-      end else begin
-        timLoadImages.Enabled := false;
-        //ImageTransferForm.ProgressBar.Position := 100;
-        //Sleep(1000);
-        //ImageTransferForm.Hide;
-      end;
-    end;
-    InTimerFn := false;
-    if self.Visible = true then begin
-      timLoadImages.Interval :=100;
-    end else begin
-      timLoadImages.Interval :=30000; //30 sec delay
-    end;
-  end;
-end;
-
+  timLoadImages.Enabled := false;
+  EnsureImageListLoaded();
+  if NumImagesAvailableOnServer = 0 then exit;
+  if (ImageIndexLastDownloaded >= (ImageInfoList.Count-1)) then exit;
+  ImageTransferForm.ProgressMsg.Caption := 'Downloading Images';
+  DownloadToCache(ImageIndexLastDownloaded+1); //Only load 1 image per timer firing.
+  SetupTab(ImageIndexLastDownloaded+1);
+  Inc(ImageIndexLastDownloaded);
+  if TabControl.TabIndex < 0 then TabControl.TabIndex := 0;
+  TabControlChange(self);
+  SetupTimer;
+end;
+
+procedure TfrmImages.SetupTimer;
+begin
+  if DownloadImagesInBackground then begin
+    timLoadImages.Interval := IMAGE_DOWNLOAD_DELAY_BACKGROUND;
+  end else begin
+    timLoadImages.Interval := IMAGE_DOWNLOAD_DELAY_FOREGROUND;
+  end;
+  timLoadImages.Enabled := true;
+end;
 
 procedure TfrmImages.EnsureImagesDownloaded(ImagesList : TStringList);
@@ -219,4 +236,18 @@
 end;
 
+procedure TfrmImages.EnsureALLImagesDownloaded;
+//This function's goal is to download ALL images in the FOREground.
+begin
+  EnsureImageListLoaded();
+  if NumImagesAvailableOnServer = 0 then exit;
+  ImageTransferForm.ProgressMsg.Caption := 'Downloading Images';
+  while (ImageIndexLastDownloaded < (ImageInfoList.Count-1)) do begin
+    DownloadToCache(ImageIndexLastDownloaded+1); //Only load 1 image per timer firing.
+    SetupTab(ImageIndexLastDownloaded+1);
+    Inc(ImageIndexLastDownloaded);
+    if TabControl.TabIndex < 0 then TabControl.TabIndex := 0;
+    TabControlChange(self);
+  end;
+end;
 
 { TPage common methods --------------------------------------------------------------------- }
@@ -241,8 +272,8 @@
 begin
   inherited;
-  InTimerFn:=false;
   LastDisplayedTIUIEN := '0';
   ImageInfoList := TList.Create;
-  CurImageToLoad := 0;
+  ClearImageList(); //sets up other needed variables.
+  DownloadImagesInBackground := true;
   CacheDir := ExtractFilePath(ParamStr(0))+ 'Cache';
   //NullImageName := ExtractFilePath(ParamStr(0)) + 'images\blank.htm';
@@ -250,10 +281,10 @@
   if not DirectoryExists(CacheDir) then ForceDirectories(CacheDir);
 
-  //UseDropBox := uTMGOptions.ReadBool('Use dropbox directory for transfers',false);
-  if uTMGOptions.ReadInteger('ImageTransferMethod',0) = 0 then begin
+  TransferMethod := TImgTransferMethod(uTMGOptions.ReadInteger('ImageTransferMethod',2));
+  {if uTMGOptions.ReadInteger('ImageTransferMethod',0) = 0 then begin
      UseDropBox := True;
   end else begin
      UseDropBox := False;
-  end;   
+  end;}
   DropBoxDir := uTMGOptions.ReadString('Dropbox directory','??');
   if DropBoxDir='??' then begin  //just on first run.
@@ -273,12 +304,10 @@
 
 procedure TfrmImages.FormShow(Sender: TObject);
-var
-  TIUIEN : AnsiString;
-                                                          
+var  TIUIEN : AnsiString;
 begin
   inherited;
   TIUIEN := IntToStr(frmNotes.lstNotes.ItemID);
-  timLoadImages.Enabled := true;
-  timLoadImages.Interval := 100;
+  DownloadImagesInBackground := false;
+  SetupTimer;
   if LastDisplayedTIUIEN <> TIUIEN then begin
     UpdateNoteInfoMemo();
@@ -380,40 +409,58 @@
         Bitmap.Height := ThumbsImageList.Height;
         index := ThumbsImageList.Add(Bitmap,nil);
-        TImageInfo(ImageInfoList[i]).TabImageIndex := index;
+        //TImageInfo(ImageInfoList[i]).TabImageIndex := index;
+        Rec.TabImageIndex := index;
         Bitmap.Free;
       end else begin
-        Ext := LowerCase(ExtractFileExt(Rec.CacheFName));
-        Ext := MidStr(Ext,2,99);
-        if   (Ext='jpg')
-          or (Ext='jpeg')
-          or (Ext='png')
-          or (Ext='tif')
-          or (Ext='tiff')
-          or (Ext='gif')
-          or (Ext='bmp') then begin
-            TImageInfo(ImageInfoList[i]).TabImageIndex := 0; //camera image
-        end else
-        if   (Ext='pdf') then begin
-            TImageInfo(ImageInfoList[i]).TabImageIndex := 1; //adobe icon
-        end else
-        if   (Ext='avi')
-          or (Ext='qt')
-          or (Ext='mpg')
-          or (Ext='mpeg') then begin
-            TImageInfo(ImageInfoList[i]).TabImageIndex := 2; //video icon
-        end else
-        if   (Ext='mp3')
-          or (Ext='wma')
-          or (Ext='au')
-          or (Ext='wav') then begin
-            TImageInfo(ImageInfoList[i]).TabImageIndex := 3; //sound icon
-        end else
-        begin
-          TImageInfo(ImageInfoList[i]).TabImageIndex := 4; // misc icon
-        end;
+        Rec.TabImageIndex := ThumbnailIndexForFName(Rec.CacheFName);
       end;
     end;
     TabControl.Tabs.Add(' ');  //add the tab.  Thumbnail should exist before this
   end;
+end;
+
+function TfrmImages.ThumbnailIndexForFName (FName : string) : integer;
+var
+  index : integer;
+  Ext : AnsiString;
+begin
+  Result := 4; //default
+  Ext := LowerCase(ExtractFileExt(FName));
+  Ext := MidStr(Ext,2,99);
+  if   (Ext='jpg')
+    or (Ext='jpeg')
+    or (Ext='png')
+    or (Ext='tif')
+    or (Ext='tiff')
+    or (Ext='gif')
+    or (Ext='bmp') then begin
+      Result := IMAGE_INDEX_IMAGE; //camera image
+  end else
+  if   (Ext='pdf') then begin
+      Result := IMAGE_INDEX_ADOBE; //adobe icon
+  end else
+  if   (Ext='avi')
+    or (Ext='qt')
+    or (Ext='mpg')
+    or (Ext='mpeg') then begin
+      Result := IMAGE_INDEX_VIDEO; //video icon
+  end else
+  if   (Ext='mp3')
+    or (Ext='wma')
+    or (Ext='au')
+    or (Ext='wav') then begin
+      Result := IMAGE_INDEX_SOUND; //sound icon
+  end else
+  begin
+    Result := IMAGE_INDEX_MISC; // misc icon
+  end;
+end;
+
+
+procedure TfrmImages.GetThumbnailBitmapForFName (FName : string; Bitmap : TBitmap);
+var index: integer;
+begin
+  index := ThumbnailIndexForFName(FName);
+  ThumbsImageList.GetBitmap(index,Bitmap);
 end;
 
@@ -437,4 +484,6 @@
     ImageInfoList.Delete(i);
   end;
+  NumImagesAvailableOnServer := NOT_YET_CHECKED_SERVER;
+  ImageIndexLastDownloaded := -1;
 end;
 
@@ -442,11 +491,11 @@
 procedure TfrmImages.EnsureImageListLoaded();
 begin
-  if CurImageToLoad = 0 then begin
+  if NumImagesAvailableOnServer = NOT_YET_CHECKED_SERVER then begin
     GetImageList();
-    if ImageInfoList.Count=0 then CurImageToLoad := -1;
   end;
 end;
 
 procedure TfrmImages.GetImageList();
+//Sets up ImageInfoList
 var
   i,j : integer;
@@ -470,5 +519,4 @@
     on E: Exception do exit;
   end;
-  //----MOVED
   StatusText('Retrieving images information...');
   CallV('MAG3 CPRS TIU NOTE', [TIUIEN]);
@@ -530,6 +578,6 @@
     end;
   end;
-   //----END MOVED
   StatusText('');
+  NumImagesAvailableOnServer := ImageInfoList.Count;
 end;
 
@@ -628,8 +676,10 @@
   result := false;  //default of failure
   if not FileExists(LocalFNamePath) then exit;
-  if UseDropBox then begin
+  //if UseDropBox then begin
+  if TransferMethod = itmDropbox then begin
     Result := UploadFileViaDropBox(LocalFNamePath,FPath,FName,CurrentImage,TotalImages);
     exit;
   end;
+  //LATER add support for itmDirect mode
   try
     InFile := TFileStream.Create(LocalFNamePath,fmOpenRead or fmShareCompat);
@@ -639,5 +689,4 @@
     // (or I could, but I'm not going to change the RPC broker...)
     ImageTransferForm.setMax(InFile.Size);
-    //ImageTransferForm.ResetStartTime;
     ImageTransferForm.ProgressMsg.Caption := 'Preparing to upload...';
     ImageTransferForm.Show;
@@ -786,8 +835,10 @@
     DeleteFile(LocalSaveFNamePath);
   end;
-  if UseDropBox then begin
+  //if UseDropBox then begin
+  if TransferMethod = itmDropbox then begin
     Result := DownloadFileViaDropBox(FPath,FName,LocalSaveFNamePath,CurrentImage,TotalImages);
     exit;
   end;
+  //LATER add support for itmDirect mode
   Result := true; //default to success;
   StatusText('Retrieving full image...');
@@ -970,10 +1021,9 @@
 begin
   ClearTabPages();
-  CurImageToLoad := 0;  //a -1 would signal that no images avail to load.
-  //this will start downloading images after 5 second delay (so that if
+  DownloadImagesInBackground := true;
+  SetupTimer;
+  //This will start downloading images after few second delay (so that if
   //user is just browsing past note, this won't waste effort.
   //If user selects images tab, then load will occur without delay.
-  timLoadImages.Enabled := true;
-  timLoadImages.Interval := 60000;  //60 sec delay  -- also set in timLoadImagesTimer
   //Note: OnTimer calls timLoadImagesTimer()
   UploadImagesButton.Enabled := EditIsActive;
@@ -1028,5 +1078,5 @@
   if not IsAbortResult(AddResult) then begin
     NewNoteSelected(true);  //force a reload to show recently added image.
-    timLoadImages.Interval := 100;
+    timLoadImages.Interval := IMAGE_DOWNLOAD_DELAY_FOREGROUND;
     Node := TORTreeNode(frmNotes.tvNotes.Selected);
     case Node.StateIndex of
@@ -1042,4 +1092,6 @@
 begin
   inherited;
+  DownloadImagesInBackground := true;
+
 //  Application.MessageBox('Here I can hide images.','title');
 end;
@@ -1277,4 +1329,22 @@
 end;
 
+function TfrmImages.GetImagesCount : integer;
+//Returns number of images possible, not just those already downloaded.
+begin
+  EnsureImageListLoaded();
+  Result := NumImagesAvailableOnServer;
+end;
+
+function TfrmImages.GetImageInfo(Index : integer) : TImageInfo;
+begin
+  if (Index > -1) and (Index < ImageInfoList.Count) then begin
+    Result := TImageInfo(ImageInfoList[Index]);
+  end else begin
+    Result := nil;
+  end;
+end;
+
+
+
 initialization
   //put init code here
