def ApplyContentApplicationCommandToMedia( parent: QW.QWidget, command: CAC.ApplicationCommand, media: typing.Collection[ ClientMedia.Media ] ):
    
    if not command.IsContentCommand():
        
        return
        
    
    service_key = command.GetContentServiceKey()
    action = command.GetContentAction()
    value = command.GetContentValue()
    
    try:
        
        service = HG.client_controller.services_manager.GetService( service_key )
        
    except HydrusExceptions.DataMissing:
        
        command_processed = False
        
        return command_processed
        
    
    service_type = service.GetServiceType()
    
    hashes = set()
    
    for m in media:
        
        hashes.add( m.GetHash() )
        
    
    if service_type in HC.REAL_TAG_SERVICES:
        
        tag = value
        
        rows = [ ( tag, hashes ) ]
        
        can_add = False
        can_pend = False
        can_delete = False
        can_petition = True
        can_rescind_pend = False
        can_rescind_petition = False
        
        for m in media:
            
            tags_manager = m.GetTagsManager()
            
            current = tags_manager.GetCurrent( service_key, ClientTags.TAG_DISPLAY_STORAGE )
            pending = tags_manager.GetPending( service_key, ClientTags.TAG_DISPLAY_STORAGE )
            petitioned = tags_manager.GetPetitioned( service_key, ClientTags.TAG_DISPLAY_STORAGE )
            
            if tag not in current:
                
                can_add = True
                
            
            if tag not in current and tag not in pending:
                
                can_pend = True
                
            
            if tag in current and action == HC.CONTENT_UPDATE_FLIP:
                
                can_delete = True
                
            
            if tag in current and tag not in petitioned and action == HC.CONTENT_UPDATE_FLIP:
                
                can_petition = True
                
            
            if tag in pending and action == HC.CONTENT_UPDATE_FLIP:
                
                can_rescind_pend = True
                
            
            if tag in petitioned:
                
                can_rescind_petition = True
                
            
        
        reason = None
        
        if service_type == HC.LOCAL_TAG:
            
            if can_add:
                
                content_update_action = HC.CONTENT_UPDATE_ADD
                
            elif can_delete:
                
                content_update_action = HC.CONTENT_UPDATE_DELETE
                
            else:
                
                return True
                
            
        else:
            
            if can_rescind_petition:
                
                content_update_action = HC.CONTENT_UPDATE_RESCIND_PETITION
                
            elif can_pend:
                
                content_update_action = HC.CONTENT_UPDATE_PEND
                
            elif can_rescind_pend:
                
                content_update_action = HC.CONTENT_UPDATE_RESCIND_PEND
                
            elif can_petition:
                
                message = 'Enter a reason for this tag to be removed. A janitor will review your petition.'
                
                from hydrus.client.gui import ClientGUIDialogs
                
                with ClientGUIDialogs.DialogTextEntry( parent, message ) as dlg:
                    
                    if dlg.exec() == QW.QDialog.Accepted:
                        
                        content_update_action = HC.CONTENT_UPDATE_PETITION
                        
                        reason = dlg.GetValue()
                        
                    else:
                        
                        return True
                        
                    
                
            else:
                
                return True
                
            
        
        content_updates = [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_MAPPINGS, content_update_action, row, reason = reason ) for row in rows ]
        
    elif service_type in ( HC.LOCAL_RATING_LIKE, HC.LOCAL_RATING_NUMERICAL ):
        
        if action in ( HC.CONTENT_UPDATE_SET, HC.CONTENT_UPDATE_FLIP ):
            
            rating = value
            
            can_set = False
            can_unset = False
            
            for m in media:
                
                ratings_manager = m.GetRatingsManager()
                
                current_rating = ratings_manager.GetRating( service_key )
                
                if current_rating == rating and action == HC.CONTENT_UPDATE_FLIP:
                    
                    can_unset = True
                    
                else:
                    
                    can_set = True
                    
                
            
            if can_set:
                
                row = ( rating, hashes )
                
            elif can_unset:
                
                row = ( None, hashes )
                
            else:
                
                return True
                
            
            content_updates = [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_RATINGS, HC.CONTENT_UPDATE_ADD, row ) ]
            
        elif action in ( HC.CONTENT_UPDATE_INCREMENT, HC.CONTENT_UPDATE_DECREMENT ):
            
            if service_type == HC.LOCAL_RATING_NUMERICAL:
                
                if action == HC.CONTENT_UPDATE_INCREMENT:
                    
                    direction = 1
                    initialisation_rating = 0.0
                    
                elif action == HC.CONTENT_UPDATE_DECREMENT:
                    
                    direction = -1
                    initialisation_rating = 1.0
                    
                
                one_star_value = service.GetOneStarValue()
                
                ratings_to_hashes = collections.defaultdict( set )
                
                for m in media:
                    
                    ratings_manager = m.GetRatingsManager()
                    
                    current_rating = ratings_manager.GetRating( service_key )
                    
                    if current_rating is None:
                        
                        new_rating = initialisation_rating
                        
                    else:
                        
                        new_rating = current_rating + ( one_star_value * direction )
                        
                        new_rating = max( min( new_rating, 1.0 ), 0.0 )
                        
                    
                    if current_rating != new_rating:
                        
                        ratings_to_hashes[ new_rating ].add( m.GetHash() )
                        
                    
                
                content_updates = [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_RATINGS, HC.CONTENT_UPDATE_ADD, ( rating, hashes ) ) for ( rating, hashes ) in ratings_to_hashes.items() ]
                
            else:
                
                return True
                
            
        
    else:
        
        return False
        
    
    if len( content_updates ) > 0:
        
        HG.client_controller.Write( 'content_updates', { service_key : content_updates } )
        
    
    return True
    def __init__(self, parent: QW.QWidget, command: CAC.ApplicationCommand,
                 shortcuts_name: str):

        ClientGUIScrolledPanels.EditPanel.__init__(self, parent)

        #

        is_custom_or_media = shortcuts_name not in ClientGUIShortcuts.SHORTCUTS_RESERVED_NAMES or shortcuts_name == 'media'

        self._panel_choice = ClientGUICommon.BetterChoice(self)

        self._panel_choice.addItem('simple command', self.PANEL_SIMPLE)

        if is_custom_or_media:

            self._panel_choice.addItem('tag command', self.PANEL_TAG)
            self._panel_choice.addItem('like/dislike rating command',
                                       self.PANEL_RATING_LIKE)
            self._panel_choice.addItem('numerical rating command',
                                       self.PANEL_RATING_NUMERICAL)
            self._panel_choice.addItem(
                'numerical rating increment/decrement command',
                self.PANEL_RATING_NUMERICAL_INCDEC)

        else:

            self._panel_choice.hide()

        self._simple_sub_panel = SimpleSubPanel(self, shortcuts_name)

        self._tag_sub_panel = TagSubPanel(self)

        self._rating_like_sub_panel = RatingLikeSubPanel(self)

        self._rating_numerical_sub_panel = RatingNumericalSubPanel(self)

        self._rating_numerical_inc_dec_sub_panel = RatingNumericalIncDecSubPanel(
            self)

        #

        data = command.GetData()

        if command.IsSimpleCommand():

            action = data

            self._simple_sub_panel.SetValue(action)

            self._panel_choice.SetValue(self.PANEL_SIMPLE)

        elif command.IsContentCommand():

            service_key = command.GetContentServiceKey()

            if HG.client_controller.services_manager.ServiceExists(
                    service_key):

                service = HG.client_controller.services_manager.GetService(
                    service_key)

                action = command.GetContentAction()
                value = command.GetContentValue()

            else:

                QW.QMessageBox.warning(
                    self, 'Warning',
                    'The service that this command relies upon no longer exists! This command will reset to a default form.'
                )

                local_tag_services = list(
                    HG.client_controller.services_manager.GetServices(
                        (HC.LOCAL_TAG, )))

                service = local_tag_services[0]

                service_key = service.GetServiceKey()
                action = HC.CONTENT_UPDATE_SET
                value = 'tag'

            service_type = service.GetServiceType()

            if service_type in HC.REAL_TAG_SERVICES:

                tag = value

                self._tag_sub_panel.SetValue(action, service_key, tag)

                self._panel_choice.SetValue(self.PANEL_TAG)

            elif service_type == HC.LOCAL_RATING_LIKE:

                rating = value

                self._rating_like_sub_panel.SetValue(action, service_key,
                                                     rating)

                self._panel_choice.SetValue(self.PANEL_RATING_LIKE)

            elif service_type == HC.LOCAL_RATING_NUMERICAL:

                if action in (HC.CONTENT_UPDATE_SET, HC.CONTENT_UPDATE_FLIP):

                    rating = value

                    self._rating_numerical_sub_panel.SetValue(
                        action, service_key, rating)

                    self._panel_choice.SetValue(self.PANEL_RATING_NUMERICAL)

                elif action in (HC.CONTENT_UPDATE_INCREMENT,
                                HC.CONTENT_UPDATE_DECREMENT):

                    distance = 1

                    self._rating_numerical_inc_dec_sub_panel.SetValue(
                        action, service_key, distance)

                    self._panel_choice.SetValue(
                        self.PANEL_RATING_NUMERICAL_INCDEC)

        #

        vbox = QP.VBoxLayout()

        QP.AddToLayout(vbox, self._panel_choice, CC.FLAGS_EXPAND_PERPENDICULAR)
        QP.AddToLayout(vbox, self._simple_sub_panel,
                       CC.FLAGS_EXPAND_PERPENDICULAR)
        QP.AddToLayout(vbox, self._tag_sub_panel,
                       CC.FLAGS_EXPAND_SIZER_PERPENDICULAR)
        QP.AddToLayout(vbox, self._rating_like_sub_panel,
                       CC.FLAGS_EXPAND_SIZER_PERPENDICULAR)
        QP.AddToLayout(vbox, self._rating_numerical_sub_panel,
                       CC.FLAGS_EXPAND_SIZER_PERPENDICULAR)
        QP.AddToLayout(vbox, self._rating_numerical_inc_dec_sub_panel,
                       CC.FLAGS_EXPAND_SIZER_PERPENDICULAR)
        vbox.addStretch(1)

        self.widget().setLayout(vbox)

        self._UpdateVisibleControls()

        #

        self._panel_choice.currentIndexChanged.connect(
            self._UpdateVisibleControls)
Exemple #3
0
def ApplyContentApplicationCommandToMedia( parent: QW.QWidget, command: CAC.ApplicationCommand, media: typing.Collection[ ClientMedia.MediaSingleton ] ):
    
    if not command.IsContentCommand():
        
        return
        
    
    service_key = command.GetContentServiceKey()
    action = command.GetContentAction()
    value = command.GetContentValue()
    
    try:
        
        service = HG.client_controller.services_manager.GetService( service_key )
        
    except HydrusExceptions.DataMissing:
        
        command_processed = False
        
        return command_processed
        
    
    service_type = service.GetServiceType()
    
    if service_type == HC.LOCAL_FILE_DOMAIN:
        
        if value is not None:
            
            source_service_key = value
            
        else:
            
            source_service_key = None
            
        
        MoveOrDuplicateLocalFiles( parent, service_key, action, media, source_service_key = source_service_key )
        
    else:
        
        if service_type in HC.REAL_TAG_SERVICES:
            
            tag = value
            
            content_updates = GetContentUpdatesForAppliedContentApplicationCommandTags( parent, service_key, service_type, action, media, tag )
            
        elif service_type in ( HC.LOCAL_RATING_LIKE, HC.LOCAL_RATING_NUMERICAL ):
            
            if action in ( HC.CONTENT_UPDATE_SET, HC.CONTENT_UPDATE_FLIP ):
                
                rating = value
                
                content_updates = GetContentUpdatesForAppliedContentApplicationCommandRatingsSetFlip( service_key, action, media, rating )
                
            elif action in ( HC.CONTENT_UPDATE_INCREMENT, HC.CONTENT_UPDATE_DECREMENT ) and service_type == HC.LOCAL_RATING_NUMERICAL:
                
                one_star_value = service.GetOneStarValue()
                
                content_updates = GetContentUpdatesForAppliedContentApplicationCommandRatingsIncDec( service_key, one_star_value, action, media )
                
            else:
                
                return True
                
            
        else:
            
            return False
            
        
        if len( content_updates ) > 0:
            
            HG.client_controller.Write( 'content_updates', { service_key : content_updates } )
            
    
    return True