CurrentPrinter.c

/*
    File:       CurrentPrinter.c
 
    Contains:   Check the default printer under GX or old printing architecture.
                Print out the name, entityType, and zone.
    
                This sample is provided as-is. It's guaranteed to work on my
                mac this instant, and nothing more. It shows how to determine
                the currently selected printer now. Since this isn't an officially
                supported endeavor, things may change in the future, and this
                snippet will be for naught.
 
    Written by: Dave Polaschek and David Hayward    
 
    Copyright:  Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/23/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1 fixed bug dealing witht
                                            copying more than 256 bytes into a pascal string.
                
 
*/
 
 
#include <stdio.h>
#include <string.h>
#include <Types.h>
#include <TextUtils.h>
#include <Files.h>
#include <Folders.h>
#include <Resources.h>
#include <Aliases.h>
#include <Gestalt.h>
#include <Memory.h>
 
 
#define gestaltGXVersion            'qdgx'          // in PrintingManager.h (old) or GXPrinting.h (new)
#define gestaltGXPrintingMgrVersion 'pmgr'          // in PrintingManager.h (old) or GXPrinting.h (new)
#define gestaltAliasMgrAttr         'alis'          // in Aliases.h
 
#define printingResourceID          0xE000          // 0xE000 = -8192
 
 
OSErr       GetCurrentPrinter(Str255 printer);
OSErr       test(void);
Boolean     gxInstalled(void);
Boolean     validPrinter(void);
 
 
/*------------------------------------------------------------------------------*\
        This function returns true if QuickDraw GX printing is available
\*------------------------------------------------------------------------------*/
Boolean gxInstalled(void)
{
    /*
    long version;
 
    if (Gestalt(gestaltGXVersion, &version) == noErr)
        if (Gestalt(gestaltGXPrintingMgrVersion, &version) == noErr)
            return(true);
    return(false);
    */
    return false;// GX printing no longer supported
}
 
 
/*------------------------------------------------------------------------------*\
        This function returns the network entity of the currently 
        selected printer, and as an added bonus, you get an error code, too!
\*------------------------------------------------------------------------------*/
OSErr GetCurrentPrinter(Str255 printer)
{
    StringHandle    theDriver;
    Handle          thePrinter;
    OSErr           theErr;
    short           resFileRefnum;
 
    theDriver = GetString(printingResourceID);
    if (!theDriver) goto BADDriver;
 
    if (gxInstalled()) {
        short   vRefNum;
        long    dirID;
        FSSpec  theFile;
        long    resSize;
 
        theErr=FindFolder(kOnSystemDisk,kDesktopFolderType,kDontCreateFolder,&vRefNum,&dirID);
        if (theErr != noErr) goto BADDriver;
 
        theErr=FSMakeFSSpec(vRefNum,dirID,*theDriver,&theFile);
        if (theErr != noErr) goto BADDriver;
 
        resFileRefnum = FSpOpenResFile(&theFile,fsRdPerm);
        if (resFileRefnum == -1) goto BADResFile;
 
        thePrinter = Get1Resource('comm', 0);
        if (!thePrinter) goto BADPrinter;
 
        if (*((long *)*thePrinter) != 'PPTL') goto BADPrinter;
 
        resSize = GetHandleSize(thePrinter)-6;
        BlockMoveData((*thePrinter)+6,printer,resSize);
    } else {
        Boolean     aliasCallsPresent;
        Boolean     aliasResourcePresent;
        long        resSize,response;
        AliasHandle theAlias;
        
        aliasCallsPresent = ((Gestalt(gestaltAliasMgrAttr,&response) == noErr) &&
            (response & 1));
 
        theAlias = (AliasHandle) GetResource('alis',printingResourceID);
        aliasResourcePresent = !!(theAlias);
        
        if (aliasCallsPresent && aliasResourcePresent) {
            FSSpec  fileSpec;
            Boolean wasChanged;
 
            theErr = ResolveAlias(NULL, theAlias,&fileSpec,&wasChanged);
            if (theErr != noErr) goto BADDriver;
 
            resFileRefnum = FSpOpenResFile(&fileSpec,fsRdPerm);
            if (resFileRefnum == -1) goto BADResFile;
        } else {
            resFileRefnum = OpenResFile(*theDriver);
            if (resFileRefnum == -1) goto BADResFile;
        }
        thePrinter = Get1Resource('PAPA', printingResourceID);
        if (!thePrinter) goto BADPrinter;
        resSize = GetHandleSize(thePrinter);
        HLock(thePrinter);
        BlockMoveData(*thePrinter,printer,resSize < 256 ? resSize : 256);
        HUnlock(thePrinter);
    }
    ReleaseResource(thePrinter);
    CloseResFile(resFileRefnum);
 
    return(noErr);
 
// Error returns
BADPrinter:
    CloseResFile(resFileRefnum);
BADResFile:
    theErr = ResError();
BADDriver:
    return(theErr);
}
 
 
/*------------------------------------------------------------------------------*\
        This function is called by the test harness. Doesn't do much except
        prove that things work without crashing.
\*------------------------------------------------------------------------------*/
OSErr test(void)
{
    OSErr   testValue;
    Str255  printerEntity;
 
    testValue = GetCurrentPrinter(printerEntity);
    if (testValue)
        return testValue;
    else {
        Str31           printer,type,zone;
        unsigned char   *currentString;
 
        currentString = printerEntity;
        BlockMove(currentString,printer,StrLength(currentString)+1);
        p2cstr(printer);
        
        currentString += StrLength(currentString) + 1;
        BlockMove(currentString,type,StrLength(currentString) + 1);
        p2cstr(type);
        
        currentString += StrLength(currentString) + 1;
        BlockMove(currentString,zone,StrLength(currentString) + 1);
        p2cstr(zone);
 
        printf("Current Printer name = %s\n"
                "EntityType = %s\n"
                "Zone = %s\n",printer,type,zone);
        return noErr;
    }
}
 
 
/*------------------------------------------------------------------------------*\
        This function tells if there's a valid printer selected (i.e. not
        aimed at a driver that's since been deleted or no printer selected
        since last System sw install) in the chooser
\*------------------------------------------------------------------------------*/
Boolean validPrinter(void)
{
    Str255  printerEntity;
    
    if (GetCurrentPrinter(printerEntity) != noErr)
        return false;
    else if (StrLength(printerEntity) == 0)
        return false;
    else
        return true;
}
 
void main(void)
{
    OSErr   testResult;
    
    testResult = test();
 
    printf("Return value from test() = %d\n",testResult);
}