On Tuesday 31 December 2002 07:12, Jim Bublitz wrote:

> name() returns a QString, so
>
>    print self.mimetype.latin1()
>
> should print the "appication/x-shellscript" and you should be able
> to use the value of self.mimetype.latin1() as a Python string.

Yes, that works but now there's another problem, see below. One of the 
problems was, I think, that findByPath() never returns None, even if you 
ask for a file that doesn't exist. It seems instead you get some undefined 
result. But this is a problem with the KDE API, which never returns 0 
(that's what the KDE documentation says).

Anyway, I think I've now built a small test case which crashes. I've 
attached it, see the comment marked with "***". Make sure that the 
filename in self.url really exists. I get "KCrash: crashing...." but the 
crash dialog doesn't appear so I don't have a backtrace. Actually it 
doesn't always crash, but at least the problem that the returned 
name().latin1() contains random characters appears each time.

Regards
 Daniel

-- 
http://www.danielnaber.de
#!/usr/bin/python

import sys
from qt import *
from kdecore import KApplication, KCmdLineArgs, KURL
from kio import KMimeType, KServiceTypeProfile

class MainWindow(QMainWindow):

	def __init__(self, ap):
		apply(QMainWindow.__init__, (self,))
		self.url = "/home/dnaber/DesktopDig/test/test1.html"

		self.mimetype = KMimeType.findByPath(self.url).name()
		self.mimestr = self.mimetype.latin1()
		print "mimestr=%s (%s)" % (self.mimestr, type(self.mimestr))
		
		self.offer = KServiceTypeProfile.preferredService(self.mimestr, "Application")
		print "offer=%s" % self.offer

		if self.offer:
			# *** the following line sometimes makes this script 
			# crash (KCrash), it also crashes without .latin1(). It 
			# also prints out "name=????ueror", the question marks 
			# are random characters when I use .utf8() instead:
			name = self.offer.name().latin1()
			print "name=%s" % name
			print "isValid=%s" % self.offer.isValid()
		return

def slotQuit():
	app.quit()

KCmdLineArgs.init(sys.argv, "testapp", "test", "0.1")
app = KApplication()
main_win = MainWindow(app)
main_win.show()
app.connect(app, SIGNAL("lastWindowClosed()"), slotQuit)
app.exec_loop()

Reply via email to