From 93a69b9cec71ca7e0140f83aeb4e31537eb9753e Mon Sep 17 00:00:00 2001 From: Norwegian Rock Cat Date: Mon, 30 Mar 2009 15:26:05 +0200 Subject: Don't send QFileOpenEvents for items on the command-line Cocoa actually has a nice feature that if you pass arguments on the command-line, Cocoa will pass those along as Open events later. This is probably how we should have handled things inside of Qt as it would have unified the file opening code. Unfortunately, we can't turn back time on this, so we need to prevent it because people probably aren't expecting it (i.e., they expect to do the parsing themselves, and not to get events later). This also means that we can send the event immediately instead of posting it, because the race that we had before no longer exists. We only do this check during launch time because that's the only time we may get bitten by it (people usually only parse the arguments once). Someday, people may actually WANT this functionality though. When that comes along, we should make it an application attribute. Task-number: 249553 Reviewed-by: Richard Moe Gustavsen --- src/gui/kernel/qcocoaapplicationdelegate_mac.mm | 24 ++++++++++++++++++++---- src/gui/kernel/qcocoaapplicationdelegate_mac_p.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/gui/kernel/qcocoaapplicationdelegate_mac.mm b/src/gui/kernel/qcocoaapplicationdelegate_mac.mm index 650ebbd..e6bd511 100644 --- a/src/gui/kernel/qcocoaapplicationdelegate_mac.mm +++ b/src/gui/kernel/qcocoaapplicationdelegate_mac.mm @@ -107,6 +107,8 @@ static void cleanupCocoaApplicationDelegate() - (id)init { self = [super init]; + if (self) + inLaunch = true; return self; } @@ -198,12 +200,26 @@ static void cleanupCocoaApplicationDelegate() return reply; } +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification +{ + Q_UNUSED(aNotification); + inLaunch = false; +} + - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames { - unsigned int ix; - for( ix = 0; ix < [filenames count]; ix++) { - NSString *fileName = [filenames objectAtIndex:ix]; - qApp->postEvent(qApp, new QFileOpenEvent(QCFString::toQString((CFStringRef)fileName))); + for (NSString *fileName in filenames) { + QString qtFileName = qt_mac_NSStringToQString(fileName); + if (inLaunch) { + // We need to be careful because Cocoa will be nice enough to take + // command line arguments and send them to us as events. Given the history + // of Qt Applications, this will result in behavior people don't want, as + // they might be doing the opening themselves with the command line parsing. + if (qApp->arguments().contains(qtFileName)) + continue; + } + QFileOpenEvent foe(qtFileName); + qt_sendSpontaneousEvent(qAppInstance(), &foe); } if (reflectionDelegate && diff --git a/src/gui/kernel/qcocoaapplicationdelegate_mac_p.h b/src/gui/kernel/qcocoaapplicationdelegate_mac_p.h index c5336f1..fca2a15 100644 --- a/src/gui/kernel/qcocoaapplicationdelegate_mac_p.h +++ b/src/gui/kernel/qcocoaapplicationdelegate_mac_p.h @@ -107,6 +107,7 @@ QT_FORWARD_DECLARE_CLASS(QApplicationPrivate); NSMenu *dockMenu; QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *qtMenuLoader; id reflectionDelegate; + bool inLaunch; } + (QT_MANGLE_NAMESPACE(QCocoaApplicationDelegate)*)sharedDelegate; - (void)setDockMenu:(NSMenu *)newMenu; -- cgit v0.12