remotes/origin/KDE/4.13 v4.13.0
Albert Astals Cid 12 years ago
parent 6112a79765
commit 49dc7dcb59
  1. 130
      shell/main.cpp

@ -33,13 +33,13 @@ static bool attachUniqueInstance(KCmdLineArgs* args)
return false; return false;
if (args->isSet("print")) if (args->isSet("print"))
iface.call("enableStartWithPrint"); iface.call("enableStartWithPrint");
if (args->isSet("page")) if (args->isSet("page"))
iface.call("openDocument", ShellUtils::urlFromArg(args->arg(0), ShellUtils::qfileExistFunc(), args->getOption("page")).url()); iface.call("openDocument", ShellUtils::urlFromArg(args->arg(0), ShellUtils::qfileExistFunc(), args->getOption("page")).url());
else else
iface.call("openDocument", ShellUtils::urlFromArg(args->arg(0), ShellUtils::qfileExistFunc()).url()); iface.call("openDocument", ShellUtils::urlFromArg(args->arg(0), ShellUtils::qfileExistFunc()).url());
if (args->isSet("raise")){ if (args->isSet("raise")) {
iface2.call("tryRaise"); iface2.call("tryRaise");
} }
return true; return true;
@ -48,72 +48,72 @@ static bool attachUniqueInstance(KCmdLineArgs* args)
// Ask an existing non-unique instance to open new tabs // Ask an existing non-unique instance to open new tabs
static bool attachExistingInstance( KCmdLineArgs* args ) static bool attachExistingInstance( KCmdLineArgs* args )
{ {
if( args->count() < 1 ) if ( args->count() < 1 )
return false; return false;
const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value(); const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
// Don't match the service without trailing "-" (unique instance) // Don't match the service without trailing "-" (unique instance)
const QString pattern = "org.kde.okular-"; const QString pattern = "org.kde.okular-";
const QString myPid = QString::number( kapp->applicationPid() ); const QString myPid = QString::number( kapp->applicationPid() );
QScopedPointer<QDBusInterface> bestService; QScopedPointer<QDBusInterface> bestService;
const int desktop = KWindowSystem::currentDesktop(); const int desktop = KWindowSystem::currentDesktop();
// Select the first instance that isn't us (metric may change in future) // Select the first instance that isn't us (metric may change in future)
foreach( const QString& service, services ) foreach ( const QString& service, services )
{
if( service.startsWith(pattern) && !service.endsWith(myPid) )
{ {
bestService.reset( new QDBusInterface(service,"/okularshell","org.kde.okular") ); if ( service.startsWith(pattern) && !service.endsWith( myPid ) )
{
bestService.reset( new QDBusInterface(service, "/okularshell", "org.kde.okular") );
// Find a window that can handle our documents // Find a window that can handle our documents
const QDBusReply<bool> reply = bestService->call( "canOpenDocs", args->count(), desktop ); const QDBusReply<bool> reply = bestService->call( "canOpenDocs", args->count(), desktop );
if( reply.isValid() && reply.value() ) if( reply.isValid() && reply.value() )
break; break;
bestService.reset(); bestService.reset();
}
} }
}
if( !bestService )
return false;
for( int i = 0; i < args->count(); ++i ) if ( !bestService )
{ return false;
QString arg = args->arg( i );
// Copy stdin to temporary file which can be opened by the existing for( int i = 0; i < args->count(); ++i )
// window. The temp file is automatically deleted after it has been
// opened. Not sure if this behavior is safe on all platforms.
QScopedPointer<QTemporaryFile> tempFile;
if( arg == "-" )
{ {
tempFile.reset( new QTemporaryFile ); QString arg = args->arg( i );
QFile stdinFile;
if( !tempFile->open() || !stdinFile.open(stdin,QIODevice::ReadOnly) )
return false;
const size_t bufSize = 1024*1024; // Copy stdin to temporary file which can be opened by the existing
QScopedPointer<char,QScopedPointerArrayDeleter<char> > buf( new char[bufSize] ); // window. The temp file is automatically deleted after it has been
size_t bytes; // opened. Not sure if this behavior is safe on all platforms.
do QScopedPointer<QTemporaryFile> tempFile;
{ if( arg == "-" )
bytes = stdinFile.read( buf.data(), bufSize ); {
tempFile->write( buf.data(), bytes ); tempFile.reset( new QTemporaryFile );
} while( bytes != 0 ); QFile stdinFile;
if( !tempFile->open() || !stdinFile.open(stdin,QIODevice::ReadOnly) )
return false;
const size_t bufSize = 1024*1024;
QScopedPointer<char,QScopedPointerArrayDeleter<char> > buf( new char[bufSize] );
size_t bytes;
do
{
bytes = stdinFile.read( buf.data(), bufSize );
tempFile->write( buf.data(), bytes );
} while( bytes != 0 );
arg = tempFile->fileName(); arg = tempFile->fileName();
} }
// Returns false if it can't fit another document // Returns false if it can't fit another document
const QDBusReply<bool> reply = bestService->call( "openDocument", arg ); const QDBusReply<bool> reply = bestService->call( "openDocument", arg );
if( !reply.isValid() || !reply.value() ) if( !reply.isValid() || !reply.value() )
return false; return false;
} }
bestService->call( "tryRaise" ); bestService->call( "tryRaise" );
return true; return true;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@ -156,22 +156,22 @@ int main(int argc, char** argv)
} }
else else
{ {
Shell* shell = new Shell( args ); Shell* shell = new Shell( args );
shell->show(); shell->show();
for( int i = 0; i < args->count(); ) for ( int i = 0; i < args->count(); )
{
if( shell->openDocument(args->arg(i)) )
++i;
else
{ {
shell = new Shell( args ); if ( shell->openDocument( args->arg(i)) )
shell->show(); ++i;
else
{
shell = new Shell( args );
shell->show();
}
} }
}
} }
} }
return app.exec(); return app.exec();
} }
// vim:ts=2:sw=2:tw=78:et /* kate: replace-tabs on; indent-width 4; */

Loading…
Cancel
Save