/src/libtomahawk/utils/TomahawkUtils_Mac.mm

http://github.com/tomahawk-player/tomahawk · Objective C++ · 110 lines · 59 code · 25 blank · 26 comment · 3 complexity · 21b1701d2133015e117249b7770af32d MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2012, Leo Franchi <lfranchi@kde.org
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "mac/FileHelpers.h"
  19. #import <Cocoa/Cocoa.h>
  20. #include "TomahawkUtils.h"
  21. #include "TomahawkUtils_Mac.h"
  22. #include <QDir>
  23. #include <QTemporaryFile>
  24. @implementation MoveDelegate
  25. -(void) setReceiver:(QObject*) object
  26. {
  27. receiver = object;
  28. }
  29. -(void) setMoveTo:(QString) p
  30. {
  31. path = p;
  32. }
  33. - (void)moveFinished
  34. {
  35. // HACK since I can't figure out how to get QuaZip to maintain executable permissions after unzip (nor find the info)
  36. // we set the binary to executable here
  37. NSLog(@"Move succeeded!, handling result");
  38. NSFileManager *manager = [[[NSFileManager alloc] init] autorelease];
  39. NSError* error;
  40. NSDictionary* attrs = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0755], NSFilePosixPermissions, nil];
  41. NSString* target = [[NSString alloc] initWithBytes:path.toUtf8() length:path.length() encoding: NSUTF8StringEncoding];
  42. NSLog(@"Changing permissions to executable for: %@", target);
  43. BOOL success = [manager setAttributes:attrs ofItemAtPath:target error:&error];
  44. if (!success) {
  45. NSLog( @"Failed to do chmod +x of moved resolver! %@", [[error userInfo] objectForKey: NSLocalizedDescriptionKey] );
  46. }
  47. if ( receiver )
  48. QMetaObject::invokeMethod(receiver, "installSucceeded", Qt::DirectConnection, Q_ARG(QString, path));
  49. [target release];
  50. }
  51. - (void)moveFailedWithError:(NSError *)error
  52. {
  53. NSLog(@"Move failed, handling result");
  54. if ( receiver )
  55. QMetaObject::invokeMethod(receiver, "installFailed", Qt::DirectConnection);
  56. }
  57. @end
  58. namespace TomahawkUtils
  59. {
  60. void
  61. bringToFront() {
  62. [NSApp activateIgnoringOtherApps:YES];
  63. }
  64. void
  65. copyWithAuthentication( const QString& srcFile, const QDir dest, QObject* receiver )
  66. {
  67. /**
  68. On OS X, we have to do the following:
  69. 1) Authenticate to be able to have write access to the /Applications folder
  70. 2) Copy file to dest
  71. 5) Call result slots on receiver object
  72. */
  73. MoveDelegate* del = [[MoveDelegate alloc] init];
  74. [del setReceiver: receiver];
  75. // Get the filename + path to save for later
  76. QFileInfo srcInfo( srcFile );
  77. const QString resultingPath = dest.absoluteFilePath( srcInfo.fileName() );
  78. [del setMoveTo: resultingPath];
  79. const QFileInfo info( srcFile );
  80. const QString destPath = dest.absoluteFilePath( info.fileName() );
  81. NSString* src = [[NSString alloc] initWithBytes: srcFile.toUtf8() length: srcFile.length() encoding: NSUTF8StringEncoding];
  82. NSString* destStr = [[NSString alloc] initWithBytes: destPath.toUtf8() length: destPath.length() encoding: NSUTF8StringEncoding];
  83. [FileHelpers moveFile:src to:destStr withDelegate:del];
  84. }
  85. }