summaryrefslogtreecommitdiffstats
path: root/kdeprint/kmprinter.cpp
blob: b18116ef81e4929f22f02f070d64d593c8f82a48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "kmprinter.h"
#include "kprinter.h"
#include "driver.h"

#include <klocale.h>
#include <kfiledialog.h>

KMPrinter::KMPrinter()
: KMObject()
{
	m_type = KMPrinter::Printer;
	m_dbentry = 0;
	m_harddefault = m_softdefault = m_ownsoftdefault = false;
	m_driver = 0;
	m_isedited = false;
	m_printercap = 0;
}

KMPrinter::KMPrinter(const KMPrinter& p)
: KMObject()
{
	m_driver = 0;	// don't copy driver structure
	m_harddefault = m_softdefault = m_ownsoftdefault = false;
	m_isedited = false;
	copy(p);
}

KMPrinter::~KMPrinter()
{
	delete m_driver;
}

void KMPrinter::copy(const KMPrinter& p)
{
	m_name = p.m_name;
	m_printername = p.m_printername;
	m_instancename = p.m_instancename;
	m_type = p.m_type;
	m_state = p.m_state;
	m_device = p.m_device;
	m_members = p.m_members;
	m_description = p.m_description;
	m_location = p.m_location;
	m_manufacturer = p.m_manufacturer;
	m_model = p.m_model;
	m_uri = p.m_uri;
	m_driverinfo = p.m_driverinfo;
	m_dbentry = p.m_dbentry;
	m_pixmap = p.m_pixmap;
	//m_harddefault = p.m_harddefault;
	//m_softdefault = p.m_softdefault;
	m_options = p.m_options;
	m_printercap = p.m_printercap;
	setDiscarded(false);
}

void KMPrinter::setDriver(DrMain *driver)
{
	delete m_driver;
	m_driver = driver;
}

DrMain* KMPrinter::takeDriver()
{
	DrMain	*dr = m_driver;
	m_driver = 0;
	return dr;
}

TQString KMPrinter::pixmap()
{
	if (!m_pixmap.isEmpty()) return m_pixmap;

	QString	str("tdeprint_printer");
	if (!isValid()) str.append("_defect");
	else
	{
		//if (isHardDefault()) str.append("_default");
		if (isClass(true)) str.append("_class");
		else if (isRemote()) str.append("_remote");
		switch (state())
		{
			case KMPrinter::Stopped:
				str.append("_stopped");
				break;
			case KMPrinter::Processing:
				str.append("_process");
				break;
			default:
				break;
		}
	}
	return str;
}

int KMPrinter::compare(KMPrinter *p1, KMPrinter *p2)
{
	if (p1 && p2)
	{
		bool	s1(p1->isSpecial()), s2(p2->isSpecial());
		if (s1 && s2) return TQString::compare(p1->name(),p2->name());
		else if (s1) return 1;
		else if (s2) return -1;
		else
		{
			bool	c1(p1->isClass(false)), c2(p2->isClass(false));
			if (c1 == c2) return TQString::compare(p1->name(),p2->name());
			else if (c1 && !c2) return -1;
			else if (!c1 && c2) return 1;
		}
	}
	return 0;
}

TQString KMPrinter::stateString() const
{
	QString	s;
	switch (state())
	{
		case KMPrinter::Idle: s = i18n("Idle"); break;
		case KMPrinter::Processing: s = i18n("Processing..."); break;
		case KMPrinter::Stopped: s = i18n("Stopped"); break;
		default: return i18n("Unknown State", "Unknown");
	}
	s += (" " + (m_state & Rejecting ? i18n("(rejecting jobs)") : i18n("(accepting jobs)")));
	return s;
}

bool KMPrinter::autoConfigure(KPrinter *printer, TQWidget *parent)
{
	// standard settings
	printer->setPrinterName(printerName());
	printer->setSearchName(name());
	// printer default settings (useful for instances)
	printer->setOptions(defaultOptions());
	// special printer case:
	//	- add command
	//	- ask for output file (if needed) using default extension.
	if (isSpecial())
	{
		if (option("kde-special-file") == "1")
		{
			// build-up default filename/directory
			TQString fName = printer->docFileName(), ext = option( "kde-special-extension" );
			if ( fName.isEmpty() )
				fName = ( printer->docName() + "." + ext );
			else
			{
				int p = fName.findRev( '.' );
				if ( p == -1 )
					fName.append( "." ).append( ext );
				else
				{
					fName.truncate( p+1 );
					fName.append( ext );
				}
			}
			fName.prepend( "/" ).prepend( printer->docDirectory() );

			// build-up file dialog
			KFileDialog *dialog = new KFileDialog (fName,
								TQString::null,
								parent,
								"filedialog",
								true);
			dialog->setOperationMode (KFileDialog::Saving);

			QString	mimetype = option("kde-special-mimetype");

			if (!mimetype.isEmpty())
			{
				TQStringList filter;
				filter << mimetype;
				filter << "all/allfiles";
				dialog->setMimeFilter (filter, mimetype);
			}
			else if (!ext.isEmpty())
				dialog->setFilter ("*." + ext + "\n*|" + i18n ("All Files"));

			if (dialog->exec ())
			{
				printer->setOutputToFile(true);
				printer->setOutputFileName(dialog->selectedFile ());
			}
			else
			{
				// action canceled
				return false;
			}
		}
		printer->setOption( "kde-isspecial", "1" );
		printer->setOption( "kde-special-command", option( "kde-special-command" ) );
	}

	return true;
}

TQString KMPrinter::deviceProtocol() const
{
	int p = m_device.find( ':' );
	if ( p != -1 )
		return m_device.left( p );
	else
		return TQString::null;
}