Prevent sending 0-byte files. Fixes #8711.
[adiumx.git] / Source / AIDockUnviewedContentPlugin.m
blob6a1ce5a2f0d30341a0f70560f71cec165f353f6a
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "AIDockUnviewedContentPlugin.h"
18 #import <Adium/AIChatControllerProtocol.h>
19 #import <Adium/AIContentControllerProtocol.h>
20 #import "AIDockController.h"
21 #import <Adium/AIPreferenceControllerProtocol.h>
22 #import <AIUtilities/AIArrayAdditions.h>
23 #import <AIUtilities/AIDictionaryAdditions.h>
24 #import <Adium/AIChat.h>
26 @interface AIDockUnviewedContentPlugin (PRIVATE)
27 - (void)removeAlert;
28 @end
30 /*!
31  * @class AIDockUnviewedContentPlugin
32  * @brief Component responsible for triggering and removing the Alert dock icon state for unviewed content
33  */
34 @implementation AIDockUnviewedContentPlugin
36 /*!
37  * @brief Install
38  */
39 - (void)installPlugin
41     //init
42     unviewedObjectsArray = [[NSMutableArray alloc] init];
43     unviewedState = NO;
45         //Register our default preferences
46     [[adium preferenceController] registerDefaults:[NSDictionary dictionaryNamed:@"DockUnviewedContentDefaults"
47                                                                                                                                                 forClass:[self class]] 
48                                                                                   forGroup:PREF_GROUP_APPEARANCE];
49         
50         //Observe pref changes
51         [[adium preferenceController] registerPreferenceObserver:self forGroup:PREF_GROUP_APPEARANCE];
54 /*!
55  * @brief Uninstall
56  */
57 - (void)uninstallPlugin
59         [[adium chatController] unregisterChatObserver:self];
60         [[adium notificationCenter] removeObserver:self];
63 #pragma mark Preference observing
64 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
65                                                         object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
67         if (!key || [key isEqualToString:KEY_ANIMATE_DOCK_ICON]) {
68                 BOOL newAnimateDockIcon = [[prefDict objectForKey:KEY_ANIMATE_DOCK_ICON] boolValue];
70                 if (newAnimateDockIcon != animateDockIcon) {
71                         animateDockIcon = newAnimateDockIcon;
72                         
73                         if (animateDockIcon) {
74                                 //Register as a chat observer (So we can catch the unviewed content status flag)
75                                 [[adium chatController] registerChatObserver:self];
76                                 
77                                 [[adium notificationCenter] addObserver:self
78                                                                                            selector:@selector(chatWillClose:)
79                                                                                                    name:Chat_WillClose object:nil];
80                                 
81                         } else {
82                                 [self removeAlert];
84                                 [[adium chatController] unregisterChatObserver:self];
85                                 [[adium notificationCenter] removeObserver:self];
86                         }
87                 }
88         }
90 /*!
91  * @brief Chat was updated
92  *
93  * Check for whether inModifiedKeys contains a change to unviewed content. If so, put the dock in the Alert state
94  * if it isn't already and there is unviewed content, or take it out of the Alert state if it is and there is none.
95  *
96  * The alert state, in the default dock icon set, is the Adium duck flapping its wings.
97  */
98 - (NSSet *)updateChat:(AIChat *)inChat keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
100     if ([inModifiedKeys containsObject:KEY_UNVIEWED_CONTENT]) {
101                 
102         if ([inChat unviewedContentCount]) {
103             //If this is the first contact with unviewed content, animate the dock
104             if (!unviewedState) {
105                 [[adium dockController] setIconStateNamed:@"Alert"];
106                 unviewedState = YES;
107             }
109             [unviewedObjectsArray addObject:inChat];
111         } else {
112             if ([unviewedObjectsArray containsObjectIdenticalTo:inChat]) {
113                 [unviewedObjectsArray removeObject:inChat];
115                 //If there are no more contacts with unviewed content, stop animating the dock
116                 if ([unviewedObjectsArray count] == 0 && unviewedState) {
117                                         [self removeAlert];
118                 }
119             }
120         }
121     }
123     return nil;
127  * @brief Remove any existing alert state
128  */
129 - (void)removeAlert
131         [[adium dockController] removeIconStateNamed:@"Alert"];
132         unviewedState = NO;
136  * @brief Respond to a chat closing
138  * Ensure that when a chat closes we remove the Alert state if necessary.
139  */
140 - (void)chatWillClose:(NSNotification *)notification
142         AIChat  *inChat = [notification object];
144         if ([unviewedObjectsArray containsObjectIdenticalTo:inChat]) {
145                 [unviewedObjectsArray removeObject:inChat];
146                 
147                 //If there are no more contacts with unviewed content, stop animating the dock
148                 if ([unviewedObjectsArray count] == 0 && unviewedState) {
149                         [self removeAlert];
150                 }
151         }
154 @end