HistoryVC.mm 10.6 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 26 27 28 29
/*
 *  Copyright (C) 2004-2015 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.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */
30
#import "HistoryVC.h"
Alexandre Lision's avatar
Alexandre Lision committed
31

32
#import <categorizedhistorymodel.h>
33
#import <QSortFilterProxyModel>
34 35
#import <callmodel.h>
#import <call.h>
36
#import <person.h>
37
#import <contactmethod.h>
Alexandre Lision's avatar
Alexandre Lision committed
38
#import <localhistorycollection.h>
Alexandre Lision's avatar
Alexandre Lision committed
39

40
#import "QNSTreeController.h"
41
#import "PersonLinkerVC.h"
Alexandre Lision's avatar
Alexandre Lision committed
42

43 44 45
#define COLUMNID_DAY			@"DayColumn"	// the single column name in our outline view
#define COLUMNID_CONTACTMETHOD	@"ContactMethodColumn"	// the single column name in our outline view
#define COLUMNID_DATE			@"DateColumn"	// the single column name in our outline view
Alexandre Lision's avatar
Alexandre Lision committed
46

47
@interface HistoryVC() <NSPopoverDelegate, KeyboardShortcutDelegate, ContactLinkedDelegate>
Alexandre Lision's avatar
Alexandre Lision committed
48

49
@property QNSTreeController *treeController;
50
@property (assign) IBOutlet RingOutlineView *historyView;
51
@property QSortFilterProxyModel *historyProxyModel;
52 53
@property (strong) NSPopover* addToContactPopover;

54
@end
Alexandre Lision's avatar
Alexandre Lision committed
55

56
@implementation HistoryVC
57 58
@synthesize treeController;
@synthesize historyView;
59
@synthesize historyProxyModel;
Alexandre Lision's avatar
Alexandre Lision committed
60 61 62 63 64 65 66 67 68 69 70

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"INIT HVC");
    }
    return self;
}

- (void)awakeFromNib
{
71 72 73 74 75
    historyProxyModel = new QSortFilterProxyModel(CategorizedHistoryModel::instance());
    historyProxyModel->setSourceModel(CategorizedHistoryModel::instance());
    historyProxyModel->setSortRole(static_cast<int>(Call::Role::Date));
    historyProxyModel->sort(0,Qt::DescendingOrder);
    treeController = [[QNSTreeController alloc] initWithQModel:historyProxyModel];
Alexandre Lision's avatar
Alexandre Lision committed
76 77 78 79

    [treeController setAvoidsEmptySelection:NO];
    [treeController setChildrenKeyPath:@"children"];

80 81 82 83
    [historyView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
    [historyView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
    [historyView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
    [historyView setTarget:self];
84
    [historyView setDoubleAction:@selector(placeHistoryCall:)];
85 86
    [historyView setContextMenuDelegate:self];
    [historyView setShortcutsDelegate:self];
Alexandre Lision's avatar
Alexandre Lision committed
87

Alexandre Lision's avatar
Alexandre Lision committed
88
    CategorizedHistoryModel::instance()->addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
Alexandre Lision's avatar
Alexandre Lision committed
89 90
}

91
- (void)placeHistoryCall:(id)sender
92 93 94
{
    if([[treeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
95 96 97
        if (!qIdx.parent().isValid()) {
            return;
        }
98
        QVariant var = historyProxyModel->data(qIdx, (int)Call::Role::ContactMethod);
99 100 101 102 103 104 105 106 107
        ContactMethod* m = qvariant_cast<ContactMethod*>(var);
        if(m){
            Call* c = CallModel::instance()->dialingCall();
            c->setDialNumber(m);
            c << Call::Action::ACCEPT;
        }
    }
}

Alexandre Lision's avatar
Alexandre Lision committed
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
#pragma mark - NSOutlineViewDelegate methods

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

// -------------------------------------------------------------------------------
//	dataCellForTableColumn:tableColumn:item
// -------------------------------------------------------------------------------
- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    NSCell *returnCell = [tableColumn dataCell];
    if(item == nil)
        return returnCell;
    return returnCell;
}

// -------------------------------------------------------------------------------
//	textShouldEndEditing:fieldEditor
// -------------------------------------------------------------------------------
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
    if ([[fieldEditor string] length] == 0)
    {
        // don't allow empty node names
        return NO;
    }
    else
    {
        return YES;
    }
}

// -------------------------------------------------------------------------------
//	shouldEditTableColumn:tableColumn:item
//
//	Decide to allow the edit of the given outline view "item".
// -------------------------------------------------------------------------------
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    return NO;
}

// -------------------------------------------------------------------------------
//	outlineView:willDisplayCell:forTableColumn:item
// -------------------------------------------------------------------------------
- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
160 161 162
    QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
    if(!qIdx.isValid())
        return;
Alexandre Lision's avatar
Alexandre Lision committed
163

164 165
    if ([[tableColumn identifier] isEqualToString:COLUMNID_DAY])
    {
166
        cell.title = historyProxyModel->data(qIdx, Qt::DisplayRole).toString().toNSString();
167 168
    } else if ([[tableColumn identifier] isEqualToString:COLUMNID_CONTACTMETHOD])
    {
169
        cell.title = historyProxyModel->data(qIdx, (int)Call::Role::Number).toString().toNSString();
170 171
    } else if ([[tableColumn identifier] isEqualToString:COLUMNID_DATE])
    {
172
        cell.title = historyProxyModel->data(qIdx, (int)Call::Role::FormattedDate).toString().toNSString();
Alexandre Lision's avatar
Alexandre Lision committed
173 174 175 176 177 178 179 180 181 182 183 184
    }
}

// -------------------------------------------------------------------------------
//	outlineViewSelectionDidChange:notification
// -------------------------------------------------------------------------------
- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
    // ask the tree controller for the current selection
    //NSLog(@"outlineViewSelectionDidChange!!");
}

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
#pragma mark - ContextMenuDelegate

- (NSMenu*) contextualMenuForIndex:(NSIndexPath*) path
{
    if([[treeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
        const auto& var = qIdx.data(static_cast<int>(Call::Role::Object));
        if (qIdx.parent().isValid() && var.isValid()) {
            if (auto call = var.value<Call *>()) {
                auto contactmethod = call->peerContactMethod();
                if (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder()) {
                    NSMenu *theMenu = [[NSMenu alloc]
                                       initWithTitle:@""];
                    [theMenu insertItemWithTitle:@"Add to contact"
                                          action:@selector(addToContact)
                                   keyEquivalent:@"a"
                                         atIndex:0];
                    return theMenu;
                }
            }
        }
    }
    return nil;
}

- (void) addToContact
{
    ContactMethod* contactmethod = nullptr;
    if([[treeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
        const auto& var = qIdx.data(static_cast<int>(Call::Role::Object));
        if (qIdx.parent().isValid() && var.isValid()) {
            if (auto call = var.value<Call *>()) {
                contactmethod = call->peerContactMethod();
            }
        }
    }

    if (self.addToContactPopover != nullptr) {
        [self.addToContactPopover performClose:self];
        self.addToContactPopover = NULL;
    } else if (contactmethod) {
227
        auto* editorVC = [[PersonLinkerVC alloc] initWithNibName:@"PersonLinker" bundle:nil];
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 269 270 271 272 273 274 275 276 277 278
        [editorVC setMethodToLink:contactmethod];
        [editorVC setContactLinkedDelegate:self];
        self.addToContactPopover = [[NSPopover alloc] init];
        [self.addToContactPopover setContentSize:editorVC.view.frame.size];
        [self.addToContactPopover setContentViewController:editorVC];
        [self.addToContactPopover setAnimates:YES];
        [self.addToContactPopover setBehavior:NSPopoverBehaviorTransient];
        [self.addToContactPopover setDelegate:self];

        [self.addToContactPopover showRelativeToRect:[historyView frameOfOutlineCellAtRow:[historyView selectedRow]] ofView:historyView preferredEdge:NSMaxXEdge];
    }
}

#pragma mark - NSPopOverDelegate

- (void)popoverDidClose:(NSNotification *)notification
{
    if (self.addToContactPopover != nullptr) {
        [self.addToContactPopover performClose:self];
        self.addToContactPopover = NULL;
    }
}

#pragma mark - ContactLinkedDelegate

- (void)contactLinked
{
    if (self.addToContactPopover != nullptr) {
        [self.addToContactPopover performClose:self];
        self.addToContactPopover = NULL;
    }
}

#pragma mark - KeyboardShortcutDelegate

- (void) onAddShortcut
{
    if([[treeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
        const auto& var = qIdx.data(static_cast<int>(Call::Role::Object));
        if (qIdx.parent().isValid() && var.isValid()) {
            if (auto call = var.value<Call *>()) {
                auto contactmethod = call->peerContactMethod();
                if (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder()) {
                    [self addToContact];
                }
            }
        }
    }
}

Alexandre Lision's avatar
Alexandre Lision committed
279
@end