;Create WinFax Import File from Fax ConDir Entries
;**********************************************************************
;* This ASPECT script creates an ASCII file from the fax entries in   *
;* a Procomm Plus 32 Connection Directory file. The information       *
;* exported to this file is intended for importing into a WinFax PRO  *
;* Phone Book using its import feature. Since WinFax also supports    *
;* voice information, the user can choose to export voice information *
;* from the Procomm Connection Directory as well.                     *
;*                                                                    *
;* The import file is created with specific requirements and          *
;* formatting:                                                        *
;*                                                                    *
;*    File type is ASCII text                                         *
;*    Field Delimiter is Comma                                        *
;*    Record Delimiter is Return (CR-LF)                              *
;*    Each record consists of the following fields specified in order *
;*       Company                                                      *
;*       First Name (which is the full name in Procomm)               *
;*       Country Code                                                 *
;*       Fax Area                                                     *
;*       Fax Number                                                   *
;*       Voice Country                                                *
;*       Voice Area                                                   *
;*       Voice Local                                                  *
;*       Notes (the name of the notes file)                           *
;*                                                                    *
;* Specify a group name to export entries from a specific group.      *
;**********************************************************************

; Revision .003

#define EXPFILE   			"WINFAXPH.TXT"
#define IMPFILE   			"PW5.DIR"
#define HELPTEXT  			"WINFAXPH.HLP"
#define SHOWME_NOSOUND 	"WFXNOSND.EXE"
#define SHOWME_SOUND		"WFXSOUND.EXE"
#define TRUE						1
#define FALSE						0

integer SHOWME_SOUND_YESNO  =	FALSE

proc main
   integer Event, IncVoice, hWnd, TaskId, ShowMe
   string ExportFile, ImportFile, ExeShowMe
   
   strfmt ImportFile "%s\%s" $PWLOCALPATH IMPFILE
   fullpath ExportFile EXPFILE
   
   IncVoice = 0
   #include "WINFAXPH.WUD"
   while 1
      if dlgevent 0 Event
         switch Event
            case 3
              if sdlgfopen "Open" ImportFile SINGLE ImportFile
                 dlgupdate 0 2
              endif
            endcase
            case 6
              if sdlgsaveas "Save As..." ExportFile ExportFile
                 dlgupdate 0 5  
              endif              
            endcase
            case 10
               disable dialogbox 0
               exportdata(ExportFile,ImportFile,IncVoice)
               enable dialogbox 0
               dlgctrlwin 0 2 hWnd
               winfocus hWnd
            endcase
            case 11
               exitwhile
            endcase
            case 12
               strfmt ExeShowMe "%s\ShowMe\WINFAXPH\%s" $PWLOCALPATH SHOWME_SOUND
               if isfile ExeShowMe
                  run ExeShowMe TaskId
                  while taskexists TaskId
                     yield
                  endwhile
               else
                  strfmt ExeShowMe "%s\ShowMe\WINFAXPH\%s" $PWLOCALPATH SHOWME_NOSOUND
                  if isfile ExeShowMe
                     run ExeShowMe
                     while taskexists TaskId
                        yield
                     endwhile
                  else
                     errormsg "SHOW ME : %s - Not Found" ExeShowMe
                  endif
               endif
            endcase
            case -1
               exitwhile
            endcase
         endswitch
      endif
      yield
   endwhile
endproc

proc exportdata
param string ExportFile, ImportFile
param integer IncVoice
   integer EntryIndex
   string EntryName, Temp, Temp2, CountryCode
   string CompanyName, FaxCountry, FaxArea, FaxNum
   string VoiceCountry, VoiceArea, VoiceNum
   
   if not dialload ImportFile
      errormsg "Dialing directory failed to load"
      halt
   endif 
      
   if fopen 0 ExportFile CREATE TEXT
      EntryIndex = 0
      while 1
        if dialname FAX EntryIndex EntryName
            if set dialentry access FAX EntryName
               CountryCode = CompanyName = FaxCountry = FaxArea = FaxNum = \
                  VoiceCountry = VoiceArea = VoiceNum = $nullstr
               fetch dialentry company CompanyName
               fetch dialentry country FaxCountry CountryCode
               fetch dialentry areacode FaxArea
               fetch dialentry phonenumber FaxNum
               if not nullstr FaxCountry
                  FaxCountry = CountryCode
               endif
               if IncVoice
                  if set dialentry access VOICE EntryName
                     fetch dialentry country VoiceCountry CountryCode
                     fetch dialentry areacode VoiceArea
                     fetch dialentry phonenumber VoiceNum
                     if not nullstr VoiceCountry
                        VoiceCountry = CountryCode
                     endif
                  endif
               endif
                  if not strcmp FaxNum ""                  
							       strfmt Temp "%s,%s,%s,%s,%s" CompanyName EntryName \
               		   FaxCountry FaxArea FaxNum
               		else
               		   strfmt Temp "%s,%s" CompanyName EntryName
               		endif
                  if IncVoice
                     if not strcmp VoiceNum ""
                        strfmt Temp2 ",%s,%s,%s" VoiceCountry VoiceArea VoiceNum
                        strcat Temp Temp2                   
                     endif
                  endif
               fputs 0 Temp
            endif
         else
            exitwhile
         endif
         ++EntryIndex
         yield
      endwhile
      usermsg "%d fax entries were exported successfully" EntryIndex
      fclose 0
   else
      errormsg "Unable to create export file"
   endif
endproc
