home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / gw / oddev.exe / DOCLIST.CPP < prev    next >
C/C++ Source or Header  |  1994-06-21  |  4KB  |  175 lines

  1. /* doclist.cpp - This file contains the implementation of the document list
  2.     object for the ODMA sample application.  This is a container object
  3.     holding lists of Document objects. */
  4.  
  5. #include <windows.h>
  6. #include "odmasamp.h"
  7.  
  8. DocumentList::DocumentList( void )
  9. {
  10.     int i;
  11.     char *buff, *entry;
  12.  
  13.     for( i = 0; i < MAXDOCS; i++ )
  14.         List[i] = NULL;
  15.  
  16.     buff = new char[2048];
  17.     if (buff) {
  18.         // Read in documents from previous session if any.
  19.         GetPrivateProfileString( "DocList", NULL, "", buff, 2048, "ODMASAMP.INI" );
  20.         for( i = 0, entry = buff; *entry; i++, entry += strlen(entry)+1 ) 
  21.             List[i] = new Document( entry );
  22.         delete[] buff;
  23.     }
  24. }
  25.  
  26. DocumentList::~DocumentList( void )
  27. {
  28.     int i;
  29.  
  30.     /* Delete all the document objects in the list. */
  31.     for( i = 0; i < MAXDOCS; i++ ) {
  32.         if (List[i])
  33.             delete List[i];
  34.     }
  35. }
  36.  
  37. Document * DocumentList::GetDocumentByIndex( int n )
  38. {
  39.     if (List[n])
  40.         return List[n];
  41.     else
  42.         return NULL;
  43. }
  44.  
  45. int DocumentList::GetDocumentIndexById( LPSTR lpszDocId )
  46. {
  47.     char FAR *lp;
  48.     int i, offset;
  49.  
  50.     //     ::ODMA\<DmsId>\<doc-specific>
  51.     offset = 7+strlen(DMSID)+1;
  52.     for( i = 0; i < MAXDOCS; i++ ) {
  53.         if (List[i] == NULL)
  54.             continue;
  55.         lp = List[i]->GetId();
  56.         if (! strcmp( lp, lpszDocId+offset ))
  57.             return i;
  58.     }
  59.     return -1;
  60. }
  61.  
  62. Document * DocumentList::GetDocumentById( LPSTR lpszDocId )
  63. {
  64.     int n;
  65.  
  66.     n = GetDocumentIndexById( lpszDocId );
  67.     if (n == -1)
  68.         return NULL;
  69.     else
  70.         return List[n];
  71. }
  72.  
  73.  
  74. ODMSTATUS DocumentList::NewDocument( LPSTR lpszFormat, LPSTR lpszDocLocation,
  75.     Document **ppDoc, DWORD dwFlags, HWND hParent )
  76. {
  77.     int i;
  78.     ODMSTATUS err;
  79.  
  80.     for( i = 0; i < MAXDOCS; i++ ) {
  81.         if (List[i] == NULL) {
  82.             // Found an empty slot.
  83.             List[i] = new Document( lpszFormat, lpszDocLocation );
  84.             if (List[i] == NULL) {
  85.                 err = ODM_E_FAIL;
  86.                 break;
  87.             }
  88.             if (dwFlags & ODM_SILENT) {
  89.                 // Non-interactive mode. Fill in default values.
  90.                 List[i]->SetInfo( ODM_AUTHOR, "Default" );
  91.                 List[i]->SetInfo( ODM_TYPE, "Default" );
  92.                 err = 0;
  93.             } else
  94.                 err = List[i]->EditAttributes( hParent );
  95.             if (err) {
  96.                 delete List[i];
  97.                 List[i] = NULL;
  98.                 break;
  99.             }
  100.             *ppDoc = List[i];
  101.             break;
  102.         }
  103.     }
  104.  
  105.     if (i == MAXDOCS) {
  106.         MessageBox( NULL, "Too many documents", DMSNAME,
  107.             MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL );
  108.         err = ODM_E_FAIL;
  109.     }
  110.     return err;
  111. }
  112.  
  113. ODMSTATUS DocumentList::CopyDocument( LPSTR lpszDocId, Document **ppDoc,
  114.     HWND hParent )
  115. {
  116.     int i;
  117.     ODMSTATUS err;
  118.     Document *pOldDoc;
  119.  
  120.     pOldDoc = GetDocumentById( lpszDocId );
  121.     if (! pOldDoc)
  122.         return ODM_E_DOCID;
  123.  
  124.     for( i = 0; i < MAXDOCS; i++ ) {
  125.         if (List[i] == NULL) {
  126.             // Found an empty slot.
  127.             List[i] = new Document( pOldDoc );
  128.             if (List[i] == NULL) {
  129.                 err = ODM_E_FAIL;
  130.                 break;
  131.             }
  132.             err = List[i]->EditAttributes( hParent );
  133.             if (err)
  134.                 break;
  135.             *ppDoc = List[i];
  136.             break;
  137.         }
  138.     }
  139.  
  140.     if (i == MAXDOCS) {
  141.         MessageBox( NULL, "Too many documents", DMSNAME,
  142.             MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL );
  143.         err = ODM_E_FAIL;
  144.     }
  145.     return err;
  146.  
  147. }
  148.  
  149. ODMSTATUS DocumentList::DeleteDocument( LPSTR lpszDocId )
  150. {
  151.     int n;
  152.  
  153.     n = GetDocumentIndexById( lpszDocId );
  154.     if (n == -1)
  155.         return ODM_E_DOCID;
  156.     delete List[n];
  157.     List[n] = NULL;
  158.     return 0;
  159. }
  160.  
  161. void DocumentList::SaveList( void )
  162. {
  163.     int i;
  164.  
  165.     // First wipe out the previous list
  166.     WritePrivateProfileString( "DocList", NULL, NULL, "ODMASAMP.INI" );
  167.  
  168.     // Now save each document's info. to the file.
  169.     for( i = 0; i < MAXDOCS; i++ ) {
  170.         if (List[i])
  171.             List[i]->SaveInfo();
  172.     }
  173. }
  174.  
  175.