/*
    SambaManger. A graphical frontend to configure the NetInfo enhanced samba.
    Copyright (C) 1998  Robert Frank

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
		
		Robert Frank, frank@ifi.unibas.ch
*/

#import "Printers.h"
#import "Controller.h"
#import "NIDirectory.h"
#import "NIProperty.h"
#import "NetInfoKeys.h"

// The property names of a printer's directory:
#define P_NAME						"name"
#define P_SAMBANAME				"netbios name"
#define P_COMMENT					"note"
#define P_REMOTE					"rm"
#define P_COMMDIR					"Comm"
#define P_COMMHOST				"HostName"
#define P_SHAREDAS				"sharedAs"
#define P_PRINTERDRIVER		"nt printer driver"
#define P_PRINTCOMMAND		"print command"
#define P_LPRMCOMMAND			"lprm command"
#define P_LPQCOMMAND			"lpq command"
#define P_LPRESUMECOMMAND	"lp resume command"
#define P_LPPAUSECOMMAND	"lp pause command"


// Class variable with the string for the open/save panel's title.
static const char *title;

@implementation Printers


// ************************************************************************
// Class methods:
+ initialize
// Called once by the run time system
{
		title = [Service stringFor:"Title:Printers"];
		return self;
}

+ new:sender at:(NXCoord *)offset
{
NIDirectory	*NIDir;
Printers		*printer = [Printers alloc];

		if ((NIDir = [NIDirectory new:sender root:SMNI_PRINTERS directory:NULL])) {
			[NIDir setDelegate:printer];
			if ([printer init:sender dirObj:NIDir delta:offset service:NULL]) {
				[NIDir setSaveTitle:title];
				return printer;
			}
			[NIDir close];
		}
		return [printer free];
}

+ open:sender at:(NXCoord *)offset
{
NIDirectory	*NIDir;
Printers		*printer = [Printers alloc];

		if ((NIDir = [NIDirectory open:sender root:SMNI_PRINTERS withTitle:title])) {		
			[NIDir setDelegate:printer];
			if ([printer init:sender dirObj:NIDir delta:offset service:[NIDir baseName]])
				return printer;

			[NIDir close];
		}
		return [printer free];
}

// ************************************************************************
//	Methods:

- (BOOL)minimumOK
{
const char *name = [textSambaName stringValue];

		if (strlen(name) > 15) {
			NXRunAlertPanel(getString("Alert:Alert"),
											getString("Message:Samba Name may have atmost 15 characters."),
											getString("Button:OK"),NULL,NULL);
			return NO;
		}
		if (*name && (strchr(name, '.') || strchr(name, ' '))) {
			NXRunAlertPanel(getString("Alert:Alert"),
											getString("Message:Samba Name may not contain dots or spaces."),
											getString("Button:OK"),NULL,NULL);
			return NO;
		}
		return YES;
}

- setupAndLoad
{		
ni_id				cdir;
ni_proplist	lprops;
int					i;
BOOL				showNote = NO;
const char	*sharedName;
NIProperty	*remoteDir, *sharedDir, *sambaNameDir;

		sambaNameDir = [ni_dirObj addString:P_SAMBANAME outlet:textSambaName];
		[ni_dirObj addString:P_NAME outlet:textUnixName];
		[ni_dirObj addString:P_COMMENT outlet:textComment];
		
		[ni_dirObj addString:P_PRINTERDRIVER outlet:textDriver];
		
		[ni_dirObj addString:P_PRINTCOMMAND outlet:textPrint];
		[ni_dirObj addString:P_LPRMCOMMAND outlet:textDelete];
		[ni_dirObj addString:P_LPQCOMMAND outlet:textShowQueue];
		[ni_dirObj addString:P_LPPAUSECOMMAND outlet:textResume];
		[ni_dirObj addString:P_LPRESUMECOMMAND outlet:textPause];

		remoteDir = [ni_dirObj addProperty:P_REMOTE];
		sharedDir = [ni_dirObj addProperty:P_SHAREDAS];

		// Check for a subdirectory P_COMMDIR with the property P_COMMHOST.
		cdir = [ni_dirObj directoryID];
		if (ni_pathsearch([ni_dirObj handle], &cdir, P_COMMDIR) == NI_OK) 
			if ((ni_read([ni_dirObj handle], &cdir, &lprops)) == NI_OK) {
				for (i = 0; i < lprops.ni_proplist_len; i++)
					showNote|= !strcmp(P_COMMHOST, lprops.ni_proplist_val[i].nip_name);
				ni_proplist_free(&lprops);
			}

		// Scan the loaded NetInfo values and update the GUI.
		[ni_dirObj scan];

		showNote|= [remoteDir values];
		
		sharedName = [sharedDir valueAt:0];
		if (*sharedName && !*[sambaNameDir valueAt:0]) {
			[sambaNameDir insertValue:sharedName];
			[textSambaName setStringValue:sharedName];
		}

		[textUnixName setEditable:NO];
		[textSambaName selectText:self];

		// We found an entry of a network printer. This could be attached to
		// a NetInfo host or be a stand alone network printer. In the first
		// case it would be better to run Samba on the host to which the printer
		// is physically attatched, in the second case it would be better to 
		// declare the printer from within Windows.
		if (showNote) {
			// Run a modal for the panel.
			[NXApp runModalFor:notePanel];
			[notePanel close];
		}

		return self;
}

// Actions from the note panel
- noteOK:sender
{
		[NXApp stopModal];
		return self;
}

// ************************************************************************
// Actions:

// ************************************************************************
// Delegates:


@end
