home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / progress / src / pwcommon.cpp < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.2 KB  |  269 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. /* pwcommon.cpp
  21.  * cross-platform progress routines
  22.  */
  23. #include "pw_public.h"
  24. #include "ntypes.h"
  25. #include "structs.h"
  26. #include "proto.h"
  27. #include "xp_mem.h"
  28. #include "fe_proto.h"
  29. #include "ctxtfunc.h"
  30. #include "xp_thrmo.h"
  31.  
  32. void pw_Progress(MWContext * cx, const char *msg);
  33. void pw_Alert(MWContext * cx, const char *msg);
  34. void pw_GraphProgressInit(MWContext *context, URL_Struct *URL_s, int32 content_length);
  35. void pw_GraphProgressDestroy(MWContext *context, URL_Struct *URL_s, int32 content_length, int32 total_bytes_read);
  36. void pw_GraphProgress(MWContext *context, URL_Struct *URL_s, int32 bytes_received, int32 bytes_since_last_time, int32 content_length);
  37. XP_Bool pw_Confirm(MWContext * context, const char * Msg);
  38. char* pw_Prompt(MWContext * context, const char * Msg, const char * dflt);
  39. char* pw_PromptWithCaption(MWContext * context, const char *caption, const char * Msg, const char * dflt);
  40. XP_Bool pw_PromptUsernameAndPassword(MWContext *,const char *,char **, char **);
  41. char * pw_PromptPassword(MWContext * context, const char * Msg);
  42. void pw_EnableClicking(MWContext * context);
  43. void pw_AllConnectionsComplete(MWContext * context);
  44. void pw_SetProgressBarPercent(MWContext *context, int32 percent);
  45. void pw_SetCallNetlibAllTheTime(MWContext *context);
  46. void pw_ClearCallNetlibAllTheTime(MWContext *context);
  47.  
  48. /* This struct encapsulates the data we need to store in our progress context */
  49.  
  50. struct pw_environment_    {
  51.     pw_ptr progressWindow;
  52.     /* data for progress total calculations */
  53.     int outstandingURLs;    /* How many outstanding URLs do we have */
  54.     XP_Bool hasUnknownSizeURLs;    /* How many outstanding URLs have unknown size */
  55.     unsigned long total_bytes;
  56.     unsigned long bytes_received;
  57.     unsigned long start_time_secs;
  58. } ;
  59.  
  60. typedef struct pw_environment_ pw_environment;
  61.  
  62. #ifdef XP_MAC
  63. #pragma export on
  64. #endif
  65.  
  66. MWContext * PW_CreateProgressContext()
  67. {
  68.     /* After the merge, it'll be XP_NewCntxt */
  69.     
  70.     pw_environment * p_context;
  71.     MWContext * newMWContext;
  72.  
  73.     p_context = (pw_environment *)XP_CALLOC(sizeof(pw_environment), 1);
  74.     if (p_context == NULL)
  75.         return NULL;
  76.  
  77.      /* initialize the new MWContext */
  78.  
  79.     newMWContext = (MWContext *) XP_CALLOC(sizeof( MWContext ), 1);
  80.     if (newMWContext == NULL)
  81.         return NULL;
  82.  
  83. //    XP_AddContextToList(newMWContext);
  84.     newMWContext->type = MWContextProgressModule;
  85.     XP_InitializeContext(newMWContext);
  86. // Assign all the functions
  87.     newMWContext->funcs = (ContextFuncs*) XP_ALLOC(sizeof(ContextFuncs));
  88.     if (newMWContext->funcs == NULL )
  89.         return NULL;
  90.  
  91.     newMWContext->funcs->Progress = pw_Progress;
  92.     newMWContext->funcs->Alert = pw_Alert;
  93.     newMWContext->funcs->GraphProgressInit = pw_GraphProgressInit;
  94.     newMWContext->funcs->GraphProgressDestroy = pw_GraphProgressDestroy;
  95.     newMWContext->funcs->GraphProgress = pw_GraphProgress;
  96.     newMWContext->funcs->Confirm = pw_Confirm;
  97.     newMWContext->funcs->Prompt = pw_Prompt;
  98.     newMWContext->funcs->PromptWithCaption = pw_PromptWithCaption;
  99.     newMWContext->funcs->PromptUsernameAndPassword = pw_PromptUsernameAndPassword;
  100.     newMWContext->funcs->PromptPassword = pw_PromptPassword;
  101.     newMWContext->funcs->PromptPassword = pw_PromptPassword;
  102.     newMWContext->funcs->EnableClicking = pw_EnableClicking;
  103.     newMWContext->funcs->AllConnectionsComplete = pw_AllConnectionsComplete;
  104.     newMWContext->funcs->SetProgressBarPercent = pw_SetProgressBarPercent;
  105.     newMWContext->funcs->SetCallNetlibAllTheTime = pw_SetCallNetlibAllTheTime;
  106.     newMWContext->funcs->ClearCallNetlibAllTheTime = pw_ClearCallNetlibAllTheTime;
  107.  
  108.  
  109.         newMWContext->mime_data = (struct MimeDisplayData *)p_context;  /* Hackily overloading a part of MWContext */
  110.     return newMWContext;
  111. }
  112.  
  113. void PW_DestroyProgressContext(MWContext * context)
  114. {
  115.     if (context)
  116.     {
  117. //        XP_RemoveContextFromList(context);
  118.         pw_ptr pw = ((pw_environment *)(context->mime_data))->progressWindow;
  119.         XP_FREEIF( context->funcs);
  120.         XP_FREEIF(context);
  121.     }
  122. }
  123.  
  124. void PW_AssociateWindowWithContext(MWContext * context, pw_ptr pw)
  125. {
  126.     pw_environment * e = (pw_environment *)(context->mime_data);
  127.     XP_Bool doReset;
  128.  
  129.     if (context->type != MWContextProgressModule)
  130.     {
  131.         XP_ASSERT(FALSE);
  132.         return;
  133.     }
  134.     doReset = (e->progressWindow != pw);
  135.     e->progressWindow = pw;
  136.     if (doReset && 
  137.         (pw != NULL))
  138.     {
  139.         PW_SetProgressText( pw, NULL);
  140.     }
  141. }
  142.  
  143. #ifdef XP_MAC
  144. #pragma export off
  145. #endif
  146.  
  147.  
  148. void pw_Progress(MWContext * cx, const char *msg)
  149. {
  150.     pw_ptr pw = ((pw_environment *)(cx->mime_data))->progressWindow;
  151.     if (pw)
  152.         PW_SetLine2(pw, msg);
  153. }
  154.  
  155. void pw_Alert(MWContext * /*cx*/, const char *msg)
  156. {
  157.     FE_Alert(NULL, msg);
  158. }
  159.  
  160. void pw_GraphProgressInit(MWContext *context, URL_Struct* /*URL_s*/, int32 content_length)
  161. {
  162.     pw_environment * pe = (pw_environment *)context->mime_data;
  163.     
  164.     pe->outstandingURLs += 1;
  165.  
  166.     if ( content_length > 0 )
  167.         pe->total_bytes += content_length;
  168.     else
  169.         pe->hasUnknownSizeURLs = TRUE;
  170.     
  171.     if (pe->outstandingURLs == 1)    /* First URL got started, set the start time */
  172.         pe->start_time_secs = XP_TIME();
  173. }
  174.  
  175. void pw_GraphProgressDestroy(MWContext *context, URL_Struct* /*URL_s*/, int32 /*content_length*/, int32 /*total_bytes_read*/)
  176. {
  177.     pw_environment * pe = (pw_environment *)context->mime_data;
  178.     pe->outstandingURLs -= 1;
  179.         
  180.     if ( pe->outstandingURLs == 0)
  181.     {
  182.         if ( pe->progressWindow )
  183.         {
  184.             PW_SetProgressText( pe->progressWindow, NULL);
  185.             PW_SetProgressRange( pe->progressWindow, 0, 0);
  186.             PW_SetProgressValue( pe->progressWindow, 0 );
  187.         }            
  188.         pe->bytes_received = 0;
  189.         pe->total_bytes = 0;
  190.         pe->start_time_secs = 0;
  191.         pe->hasUnknownSizeURLs = FALSE; 
  192.     }
  193. }
  194.  
  195. void pw_GraphProgress(MWContext *context, URL_Struct* /*URL_s*/, int32 /*bytes_received*/, int32 bytes_since_last_time, int32 /*content_length*/)
  196. {
  197.     pw_environment * pe = (pw_environment *)context->mime_data;
  198.     const char * progressText;
  199.  
  200.     pe->bytes_received += bytes_since_last_time;
  201.     if (pe->progressWindow)
  202.     {
  203.         progressText = XP_ProgressText( pe->hasUnknownSizeURLs ? 0 : pe->total_bytes, 
  204.                                     pe->bytes_received, 
  205.                                     pe->start_time_secs,
  206.                                     XP_TIME());
  207.         PW_SetProgressText( pe->progressWindow, progressText);
  208.     }
  209. }
  210.  
  211. void pw_SetProgressBarPercent(MWContext *context, int32 percent)
  212. {
  213.     pw_environment * pe = (pw_environment *)context->mime_data;
  214.     if (pe->progressWindow)
  215.     {
  216.         PW_SetProgressValue( pe->progressWindow, percent);
  217.     }
  218. }
  219.  
  220. XP_Bool pw_Confirm(MWContext* /*context*/, const char* /*Msg*/)
  221. {
  222.     XP_ASSERT(FALSE);
  223.     return FALSE;
  224. //    return FE_Confirm(NULL, Msg);
  225. }
  226.  
  227. char* pw_Prompt(MWContext * /*context*/, const char * /*Msg*/, const char * /*dflt*/)
  228. {
  229.     XP_ASSERT(FALSE);
  230.     return NULL;
  231. //    return FE_Prompt(NULL, Msg, dflt);
  232. }
  233.  
  234. char* pw_PromptWithCaption(MWContext * /*context */, const char * /* caption */, const char * /*Msg*/, const char * /*dflt*/)
  235. {
  236.     return NULL;    // FE_PromptWithCaption(NULL, caption, Msg, dflt);
  237. }
  238.  
  239. XP_Bool pw_PromptUsernameAndPassword(MWContext *,const char * /* prompt */,char ** /* username */ , char ** /* password */)
  240. {
  241.     //return FE_PromptUsernameAndPassword(NULL, prompt, username, password);
  242.     return FALSE;
  243. }
  244.  
  245. char * pw_PromptPassword(MWContext * /*context*/, const char * /*Msg*/)
  246. {
  247.     // return FE_PromptPassword(NULL, Msg);
  248.     return NULL;
  249. }
  250.  
  251. void pw_EnableClicking(MWContext * /*context*/)
  252. {
  253. }
  254.  
  255. void pw_AllConnectionsComplete(MWContext * /*context*/)
  256. {
  257. }
  258.  
  259. void pw_SetCallNetlibAllTheTime(MWContext * /*context*/)
  260. {
  261.     XP_ASSERT(FALSE);
  262. }
  263.  
  264. void pw_ClearCallNetlibAllTheTime(MWContext * /*context*/)
  265. {
  266.     XP_ASSERT(FALSE);
  267. }
  268.  
  269.