AccMediaVC.mm 11.4 KB
Newer Older
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
/*
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
#import "AccMediaVC.h"

///Qt
#import <QSortFilterProxyModel>
#import <qitemselectionmodel.h>

///LRC
Edric Milaret's avatar
Edric Milaret committed
26
#import <codecmodel.h>
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
#import <accountmodel.h>
#import <ringtonemodel.h>
#import <ringtone.h>

@interface AccMediaVC ()

@property QNSTreeController* audioTreeController;
@property QNSTreeController* videoTreeController;
@property (unsafe_unretained) IBOutlet NSPopUpButton* ringtonePopUpButton;
@property (unsafe_unretained) IBOutlet NSButton* enableRingtone;
@property (unsafe_unretained) IBOutlet NSButton* playRingtone;
@property (unsafe_unretained) IBOutlet NSButton *toggleVideoButton;
@property (unsafe_unretained) IBOutlet NSOutlineView* audioCodecView;
@property (unsafe_unretained) IBOutlet NSOutlineView* videoCodecView;
@property (unsafe_unretained) IBOutlet NSView* videoPanelContainer;

@end

@implementation AccMediaVC
@synthesize audioTreeController, videoTreeController;
@synthesize audioCodecView, videoCodecView;
@synthesize videoPanelContainer;
@synthesize ringtonePopUpButton, enableRingtone, playRingtone, toggleVideoButton;

NSInteger const TAG_CHECK       =   100;
NSInteger const TAG_NAME        =   200;
NSInteger const TAG_DETAILS     =   300;

NSString*  const ID_AUDIO       =   @"audioview";
NSString*  const ID_VIDEO       =   @"videoview";

- (void) loadView
{
    [super loadView];
    NSLog(@"INIT Media VC");
    QObject::connect(AccountModel::instance().selectionModel(),
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;
                         [self loadAccount];
                     });

    QObject::connect(&RingtoneModel::instance(),
                     &QAbstractItemModel::dataChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;

                         NSString* label;
                         if (!RingtoneModel::instance().isPlaying()) {
                             label = NSLocalizedString(@"Play", @"Button label");
                         } else {
                             label = NSLocalizedString(@"Pause", @"Button label");
                         }
                         [playRingtone setTitle:label];
                     });
}

- (void)loadAccount
{
    // AUDIO
    [self loadAudioPrefs];

    // VIDEO
    [self loadVideoPrefs];

}

#pragma Audio Preferences method

- (void) loadAudioPrefs
{
    auto account = AccountModel::instance().selectedAccount();
    audioTreeController = [[QNSTreeController alloc] initWithQModel:account->codecModel()->audioCodecs()];
    [audioTreeController setAvoidsEmptySelection:NO];
    [audioTreeController setChildrenKeyPath:@"children"];
    [audioCodecView bind:@"content" toObject:audioTreeController withKeyPath:@"arrangedObjects" options:nil];
    [audioCodecView bind:@"sortDescriptors" toObject:audioTreeController withKeyPath:@"sortDescriptors" options:nil];
    [audioCodecView bind:@"selectionIndexPaths" toObject:audioTreeController withKeyPath:@"selectionIndexPaths" options:nil];
    [audioCodecView setIdentifier:ID_AUDIO];

    QModelIndex qIdx = RingtoneModel::instance().selectionModel(account)->currentIndex();
    [ringtonePopUpButton removeAllItems];
    [ringtonePopUpButton addItemWithTitle:RingtoneModel::instance().data(qIdx, Qt::DisplayRole).toString().toNSString()];

    [enableRingtone setState:account->isRingtoneEnabled()];
    [ringtonePopUpButton setEnabled:account->isRingtoneEnabled()];
}

- (IBAction)startStopRingtone:(id)sender {
    auto qIdx = RingtoneModel::instance().selectionModel(AccountModel::instance().selectedAccount())->currentIndex();
    RingtoneModel::instance().play(qIdx);
}

- (IBAction)toggleRingtoneEnabled:(id)sender {
    AccountModel::instance().selectedAccount()->setRingtoneEnabled([sender state]);
    [ringtonePopUpButton setEnabled:[sender state]];
}

- (IBAction)chooseRingtone:(id)sender {
    int index = [sender indexOfSelectedItem];
    QModelIndex qIdx = RingtoneModel::instance().index(index, 0);
    RingtoneModel::instance().selectionModel(AccountModel::instance().selectedAccount())->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
}

- (IBAction)moveAudioCodecUp:(id)sender {
    if([[audioTreeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [audioTreeController toQIdx:[audioTreeController selectedNodes][0]];
        if(!qIdx.isValid())
            return;

        QMimeData* mime = AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->mimeData(QModelIndexList() << qIdx);
        AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->dropMimeData(mime, Qt::MoveAction, qIdx.row() - 1, 0, QModelIndex());
    }
}

- (IBAction)moveAudioCodecDown:(id)sender {
    if([[audioTreeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [audioTreeController toQIdx:[audioTreeController selectedNodes][0]];
        if(!qIdx.isValid())
            return;

        QMimeData* mime = AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->mimeData(QModelIndexList() << qIdx);
        AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->dropMimeData(mime, Qt::MoveAction, qIdx.row() + 1, 0, QModelIndex());
    }
}

- (IBAction)toggleAudioCodec:(NSButton*)sender {
    NSInteger row = [audioCodecView rowForView:sender];
    QModelIndex qIdx = AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->index(row, 0, QModelIndex());
    AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->setData(qIdx, sender.state == NSOnState ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}

#pragma Video Preferences method

-(void) loadVideoPrefs
{
    auto account = AccountModel::instance().selectedAccount();
    videoTreeController = [[QNSTreeController alloc] initWithQModel:account->codecModel()->videoCodecs()];
    [videoTreeController setAvoidsEmptySelection:NO];
    [videoTreeController setChildrenKeyPath:@"children"];

    [videoCodecView setIdentifier:ID_VIDEO];
    [videoCodecView bind:@"content" toObject:videoTreeController withKeyPath:@"arrangedObjects" options:nil];
    [videoCodecView bind:@"sortDescriptors" toObject:videoTreeController withKeyPath:@"sortDescriptors" options:nil];
    [videoCodecView bind:@"selectionIndexPaths" toObject:videoTreeController withKeyPath:@"selectionIndexPaths" options:nil];
    [videoPanelContainer setHidden:!account->isVideoEnabled()];
    [toggleVideoButton setState:account->isVideoEnabled()?NSOnState:NSOffState];

}

- (IBAction)toggleVideoEnabled:(id)sender {
    AccountModel::instance().selectedAccount()->setVideoEnabled([sender state] == NSOnState);
    [videoPanelContainer setHidden:!AccountModel::instance().selectedAccount()->isVideoEnabled()];
}

- (IBAction)toggleVideoCodec:(NSButton*)sender {
    NSInteger row = [videoCodecView rowForView:sender];
    QModelIndex qIdx = AccountModel::instance().selectedAccount()->codecModel()->videoCodecs()->index(row, 0, QModelIndex());
    AccountModel::instance().selectedAccount()->codecModel()->videoCodecs()->setData(qIdx, sender.state == NSOnState ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}

- (IBAction)moveVideoCodecUp:(id)sender {

    if([[videoTreeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [videoTreeController toQIdx:[videoTreeController selectedNodes][0]];
        if(!qIdx.isValid())
            return;

        QMimeData* mime = AccountModel::instance().selectedAccount()->codecModel()->mimeData(QModelIndexList() << qIdx);
        AccountModel::instance().selectedAccount()->codecModel()->dropMimeData(mime, Qt::MoveAction, qIdx.row() - 1, 0, QModelIndex());
    }
}

- (IBAction)moveVideoCodecDown:(id)sender {
    if([[videoTreeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [videoTreeController toQIdx:[videoTreeController selectedNodes][0]];
        if(!qIdx.isValid())
            return;

        QMimeData* mime = AccountModel::instance().selectedAccount()->codecModel()->mimeData(QModelIndexList() << qIdx);
        AccountModel::instance().selectedAccount()->codecModel()->dropMimeData(mime, Qt::MoveAction, qIdx.row() + 1, 0, QModelIndex());
    }
}


#pragma mark - NSOutlineViewDelegate methods

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
{
    return YES;
}

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    NSTableView* result = [outlineView makeViewWithIdentifier:@"CodecView" owner:self];

    QModelIndex qIdx;
    if ([outlineView.identifier isEqualToString:ID_AUDIO])
        qIdx = [audioTreeController toQIdx:((NSTreeNode*)item)];
    else
        qIdx = [videoTreeController toQIdx:((NSTreeNode*)item)];

    if(!qIdx.isValid())
        return result;
    NSTextField* name = [result viewWithTag:TAG_NAME];
    NSTextField* details = [result viewWithTag:TAG_DETAILS];
    NSButton* check = [result viewWithTag:TAG_CHECK];

    if ([outlineView.identifier isEqualToString:ID_AUDIO]) {
        [name setStringValue:AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->data(qIdx, CodecModel::Role::NAME).toString().toNSString()];
        [details setStringValue:[NSString stringWithFormat:@"%@ Hz", AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->data(qIdx, CodecModel::Role::SAMPLERATE).toString().toNSString()]];
        [check setState:AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->data(qIdx, Qt::CheckStateRole).value<BOOL>()?NSOnState:NSOffState];
    } else {
        [name setStringValue:AccountModel::instance().selectedAccount()->codecModel()->videoCodecs()->data(qIdx, CodecModel::Role::NAME).toString().toNSString()];
        [check setState:AccountModel::instance().selectedAccount()->codecModel()->videoCodecs()->data(qIdx, Qt::CheckStateRole).value<BOOL>()?NSOnState:NSOffState];
    }

    return result;
}

#pragma mark - NSMenuDelegate methods

- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
{
    QModelIndex qIdx;
    qIdx = RingtoneModel::instance().index(index, 0);
    [item setTitle:RingtoneModel::instance().data(qIdx, Qt::DisplayRole).toString().toNSString()];

    if (qIdx == RingtoneModel::instance().selectionModel(AccountModel::instance().selectedAccount())->currentIndex()) {
        [ringtonePopUpButton selectItem:item];
    }
    return YES;
}

- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
{
    return RingtoneModel::instance().rowCount();
}

@end