Philipp Hörist pushed to branch master at gajim / gajim

Commits:

4 changed files:

Changes:

  • gajim/chat_control.py
    ... ... @@ -1424,24 +1424,24 @@ class ChatControl(ChatControlBase):
    1424 1424
     
    
    1425 1425
             local_old_kind = None
    
    1426 1426
             self.conv_textview.just_cleared = True
    
    1427
    -        for row in rows: # row[0] time, row[1] has kind, row[2] the message, row[3] subject, row[4] additional_data
    
    1428
    -            msg = row[2]
    
    1429
    -            additional_data = row[4]
    
    1427
    +        for row in rows: # time, kind, message, subject, additional_data
    
    1428
    +            msg = row.message
    
    1429
    +            additional_data = row.additional_data
    
    1430 1430
                 if not msg: # message is empty, we don't print it
    
    1431 1431
                     continue
    
    1432
    -            if row[1] in (KindConstant.CHAT_MSG_SENT,
    
    1432
    +            if row.kind in (KindConstant.CHAT_MSG_SENT,
    
    1433 1433
                                 KindConstant.SINGLE_MSG_SENT):
    
    1434 1434
                     kind = 'outgoing'
    
    1435 1435
                     name = self.get_our_nick()
    
    1436
    -            elif row[1] in (KindConstant.SINGLE_MSG_RECV,
    
    1436
    +            elif row.kind in (KindConstant.SINGLE_MSG_RECV,
    
    1437 1437
                                 KindConstant.CHAT_MSG_RECV):
    
    1438 1438
                     kind = 'incoming'
    
    1439 1439
                     name = self.contact.get_shown_name()
    
    1440
    -            elif row[1] == KindConstant.ERROR:
    
    1440
    +            elif row.kind == KindConstant.ERROR:
    
    1441 1441
                     kind = 'status'
    
    1442 1442
                     name = self.contact.get_shown_name()
    
    1443 1443
     
    
    1444
    -            tim = float(row[0])
    
    1444
    +            tim = float(row.time)
    
    1445 1445
     
    
    1446 1446
                 if gajim.config.get('restored_messages_small'):
    
    1447 1447
                     small_attr = ['small']
    
    ... ... @@ -1450,14 +1450,14 @@ class ChatControl(ChatControlBase):
    1450 1450
                 xhtml = None
    
    1451 1451
                 if msg.startswith('<body '):
    
    1452 1452
                     xhtml = msg
    
    1453
    -            if row[3]:
    
    1453
    +            if row.subject:
    
    1454 1454
                     msg = _('Subject: %(subject)s\n%(message)s') % \
    
    1455
    -                    {'subject': row[3], 'message': msg}
    
    1455
    +                    {'subject': row.subject, 'message': msg}
    
    1456 1456
                 ChatControlBase.print_conversation_line(self, msg, kind, name,
    
    1457 1457
                     tim, small_attr, small_attr + ['restored_message'],
    
    1458 1458
                     small_attr + ['restored_message'], False,
    
    1459 1459
                     old_kind=local_old_kind, xhtml=xhtml, additional_data=additional_data)
    
    1460
    -            if row[2].startswith('/me ') or row[2].startswith('/me\n'):
    
    1460
    +            if row.message.startswith('/me ') or row.message.startswith('/me\n'):
    
    1461 1461
                     local_old_kind = None
    
    1462 1462
                 else:
    
    1463 1463
                     local_old_kind = kind
    

  • gajim/chat_control_base.py
    ... ... @@ -444,7 +444,12 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools):
    444 444
         def get_encryption_state(self):
    
    445 445
             config_key = '%s-%s' % (self.account, self.contact.jid)
    
    446 446
             state = gajim.config.get_per('encryption', config_key, 'encryption')
    
    447
    -        return state or None
    
    447
    +        if not state:
    
    448
    +            return None
    
    449
    +        if state not in gajim.plugin_manager.encryption_plugins:
    
    450
    +            self.set_encryption_state(None)
    
    451
    +            return None
    
    452
    +        return state
    
    448 453
     
    
    449 454
         def set_encryption_menu_icon(self):
    
    450 455
             for child in self.encryption_menu.get_children():
    

  • gajim/common/logger.py
    ... ... @@ -121,6 +121,20 @@ class Logger:
    121 121
             gajim.ged.register_event_handler('gc-message-received',
    
    122 122
                 ged.POSTCORE, self._nec_gc_message_received)
    
    123 123
     
    
    124
    +    @staticmethod
    
    125
    +    def namedtuple_factory(cursor, row):
    
    126
    +        """
    
    127
    +        Usage:
    
    128
    +        con.row_factory = namedtuple_factory
    
    129
    +        """
    
    130
    +        fields = [col[0] for col in cursor.description]
    
    131
    +        Row = namedtuple("Row", fields)
    
    132
    +        named_row = Row(*row)
    
    133
    +        if 'additional_data' in fields:
    
    134
    +            named_row = named_row._replace(
    
    135
    +                additional_data=json.loads(named_row.additional_data))
    
    136
    +        return named_row
    
    137
    +
    
    124 138
         def dispatch(self, event, error):
    
    125 139
             gajim.ged.raise_event(event, None, str(error))
    
    126 140
     
    
    ... ... @@ -145,6 +159,7 @@ class Logger:
    145 159
             self.con = sqlite.connect(LOG_DB_FILE, timeout=20.0,
    
    146 160
                     isolation_level='IMMEDIATE')
    
    147 161
             os.chdir(back)
    
    162
    +        self.con.row_factory = self.namedtuple_factory
    
    148 163
             self.cur = self.con.cursor()
    
    149 164
             self.set_synchronous(False)
    
    150 165
     
    
    ... ... @@ -191,18 +206,16 @@ class Logger:
    191 206
         def get_jids_already_in_db(self):
    
    192 207
             try:
    
    193 208
                 self.cur.execute('SELECT jid FROM jids')
    
    194
    -            # list of tuples: [('aaa@bbb',), ('cc@dd',)]
    
    195 209
                 rows = self.cur.fetchall()
    
    196 210
             except sqlite.DatabaseError:
    
    197 211
                 raise exceptions.DatabaseMalformed(LOG_DB_PATH)
    
    198 212
             self.jids_already_in = []
    
    199 213
             for row in rows:
    
    200
    -            # row[0] is first item of row (the only result here, the jid)
    
    201
    -            if row[0] == '':
    
    214
    +            if not row.jid:
    
    202 215
                     # malformed jid, ignore line
    
    203 216
                     pass
    
    204 217
                 else:
    
    205
    -                self.jids_already_in.append(row[0])
    
    218
    +                self.jids_already_in.append(row.jid)
    
    206 219
     
    
    207 220
         def get_jids_in_db(self):
    
    208 221
             return self.jids_already_in
    
    ... ... @@ -232,7 +245,7 @@ class Logger:
    232 245
             if row is None:
    
    233 246
                 return None
    
    234 247
             else:
    
    235
    -            if row[0] == JIDConstant.ROOM_TYPE:
    
    248
    +            if row.type == JIDConstant.ROOM_TYPE:
    
    236 249
                     return True
    
    237 250
                 return False
    
    238 251
     
    
    ... ... @@ -253,7 +266,7 @@ class Logger:
    253 266
                 self.cur.execute('SELECT jid_id FROM jids WHERE jid=?', [jid])
    
    254 267
                 row = self.cur.fetchone()
    
    255 268
                 if row:
    
    256
    -                return row[0]
    
    269
    +                return row.jid_id
    
    257 270
             # oh! a new jid :), we add it now
    
    258 271
             if typestr == 'ROOM':
    
    259 272
                 typ = JIDConstant.ROOM_TYPE
    
    ... ... @@ -476,8 +489,8 @@ class Logger:
    476 489
             except Exception:
    
    477 490
                 unread_results = []
    
    478 491
             for message in unread_results:
    
    479
    -            msg_log_id = message[0]
    
    480
    -            shown = message[1]
    
    492
    +            msg_log_id = message.message_id
    
    493
    +            shown = message.shown
    
    481 494
                 # here we get infos for that message, and related jid from jids table
    
    482 495
                 # do NOT change order of SELECTed things, unless you change function(s)
    
    483 496
                 # that called this function
    
    ... ... @@ -488,15 +501,13 @@ class Logger:
    488 501
                         WHERE logs.log_line_id = %d AND logs.jid_id = jids.jid_id
    
    489 502
                         ''' % msg_log_id
    
    490 503
                         )
    
    491
    -            results = self.cur.fetchall()
    
    504
    +            results = self.cur.fetchone()
    
    492 505
                 if len(results) == 0:
    
    493 506
                     # Log line is no more in logs table. remove it from unread_messages
    
    494 507
                     self.set_read_messages([msg_log_id])
    
    495 508
                     continue
    
    496
    -            results[0] = list(results[0])
    
    497
    -            results[0][5] = json.loads(results[0][5])
    
    498
    -            results[0].append(shown)
    
    499
    -            all_messages.append(results[0])
    
    509
    +
    
    510
    +            all_messages.append((results, shown))
    
    500 511
             return all_messages
    
    501 512
     
    
    502 513
         def write(self, kind, jid, message=None, show=None, tim=None, subject=None,
    
    ... ... @@ -619,12 +630,7 @@ class Logger:
    619 630
                     KindConstant.CHAT_MSG_SENT, KindConstant.ERROR, timeout,
    
    620 631
                     restore_how_many, pending_how_many), jid_tuple)
    
    621 632
     
    
    622
    -            results = self.cur.fetchall()
    
    623
    -            messages = []
    
    624
    -            for entry in results:
    
    625
    -                additional_data = json.loads(entry[4])
    
    626
    -                parsed_entry = entry[:4] + (additional_data, ) + entry[5:]
    
    627
    -                messages.append(parsed_entry)
    
    633
    +            messages = self.cur.fetchall()
    
    628 634
             except sqlite.DatabaseError:
    
    629 635
                 self.dispatch('DB_ERROR',
    
    630 636
                               exceptions.DatabaseMalformed(LOG_DB_PATH))
    
    ... ... @@ -643,10 +649,6 @@ class Logger:
    643 649
             start_of_day = int(time.mktime(local_time))
    
    644 650
             return start_of_day
    
    645 651
     
    
    646
    -    Message = namedtuple('Message',
    
    647
    -            ['contact_name', 'time', 'kind', 'show', 'message', 'subject',
    
    648
    -             'additional_data', 'log_line_id'])
    
    649
    -
    
    650 652
         def get_conversation_for_date(self, jid, year, month, day, account):
    
    651 653
             """
    
    652 654
             Load the complete conversation with a given jid on a specific date
    
    ... ... @@ -680,11 +682,7 @@ class Logger:
    680 682
                 ORDER BY time
    
    681 683
                 ''' % (where_sql, start_of_day, last_second_of_day), jid_tuple)
    
    682 684
     
    
    683
    -        results = [self.Message(*row) for row in self.cur.fetchall()]
    
    684
    -        for message in results:
    
    685
    -            message._replace(additional_data=json.loads(message.additional_data))
    
    686
    -
    
    687
    -        return results
    
    685
    +        return self.cur.fetchall()
    
    688 686
     
    
    689 687
         def search_log(self, jid, query, account, year=None, month=None, day=None):
    
    690 688
             """
    
    ... ... @@ -728,11 +726,7 @@ class Logger:
    728 726
                 ORDER BY time
    
    729 727
                 ''' % (where_sql, like_sql), jid_tuple)
    
    730 728
     
    
    731
    -        results = [self.Message(*row) for row in self.cur.fetchall()]
    
    732
    -        for message in results:
    
    733
    -            message._replace(additional_data=json.loads(message.additional_data))
    
    734
    -
    
    735
    -        return results
    
    729
    +        return self.cur.fetchall()
    
    736 730
     
    
    737 731
         def get_days_with_logs(self, jid, year, month, max_day, account):
    
    738 732
             """
    
    ... ... @@ -756,7 +750,7 @@ class Logger:
    756 750
             # and take only one of the same values (distinct)
    
    757 751
             # Now we have timestamps of time 0:00 of every day with logs
    
    758 752
             self.cur.execute('''
    
    759
    -            SELECT DISTINCT time/(86400)*86400 FROM logs
    
    753
    +            SELECT DISTINCT time/(86400)*86400 as time FROM logs
    
    760 754
                 WHERE (%s)
    
    761 755
                 AND time BETWEEN %d AND %d
    
    762 756
                 AND kind NOT IN (%d, %d)
    
    ... ... @@ -767,7 +761,7 @@ class Logger:
    767 761
     
    
    768 762
             # convert timestamps to day of month
    
    769 763
             for line in result:
    
    770
    -            days_with_logs[0:0]=[time.gmtime(line[0])[2]]
    
    764
    +            days_with_logs[0:0]=[time.gmtime(line.time)[2]]
    
    771 765
     
    
    772 766
             return days_with_logs
    
    773 767
     
    
    ... ... @@ -788,7 +782,7 @@ class Logger:
    788 782
                 where_sql = 'jid_id = ?'
    
    789 783
                 jid_tuple = (jid_id,)
    
    790 784
             self.cur.execute('''
    
    791
    -            SELECT MAX(time) FROM logs
    
    785
    +            SELECT MAX(time) as time FROM logs
    
    792 786
                 WHERE (%s)
    
    793 787
                 AND kind NOT IN (%d, %d)
    
    794 788
                 ''' % (where_sql, KindConstant.STATUS, KindConstant.GCSTATUS),
    
    ... ... @@ -796,7 +790,7 @@ class Logger:
    796 790
     
    
    797 791
             results = self.cur.fetchone()
    
    798 792
             if results is not None:
    
    799
    -            result = results[0]
    
    793
    +            result = results.time
    
    800 794
             else:
    
    801 795
                 result = None
    
    802 796
             return result
    
    ... ... @@ -819,7 +813,7 @@ class Logger:
    819 813
     
    
    820 814
             results = self.cur.fetchone()
    
    821 815
             if results is not None:
    
    822
    -            result = results[0]
    
    816
    +            result = results.time
    
    823 817
             else:
    
    824 818
                 result = None
    
    825 819
             return result
    
    ... ... @@ -870,10 +864,9 @@ class Logger:
    870 864
                 return
    
    871 865
             self.cur.execute(
    
    872 866
                     'SELECT type from transports_cache WHERE transport = "%s"' % jid)
    
    873
    -        results = self.cur.fetchall()
    
    867
    +        results = self.cur.fetchone()
    
    874 868
             if results:
    
    875
    -            result = results[0][0]
    
    876
    -            if result == type_id:
    
    869
    +            if results.type == type_id:
    
    877 870
                     return
    
    878 871
                 sql = 'UPDATE transports_cache SET type = %d WHERE transport = "%s"' %\
    
    879 872
                         (type_id, jid)
    
    ... ... @@ -893,8 +886,8 @@ class Logger:
    893 886
                 return {}
    
    894 887
             answer = {}
    
    895 888
             for result in results:
    
    896
    -            answer[result[0]] = self.convert_api_values_to_human_transport_type(
    
    897
    -                    result[1])
    
    889
    +            answer[result.transport] = self.convert_api_values_to_human_transport_type(
    
    890
    +                    result.type)
    
    898 891
             return answer
    
    899 892
     
    
    900 893
         # A longer note here:
    
    ... ... @@ -924,16 +917,16 @@ class Logger:
    924 917
     
    
    925 918
             # list of corrupted entries that will be removed
    
    926 919
             to_be_removed = []
    
    927
    -        for hash_method, hash_, data in self.cur:
    
    920
    +        for row in self.cur:
    
    928 921
                 # for each row: unpack the data field
    
    929 922
                 # (format: (category, type, name, category, type, name, ...
    
    930 923
                 #   ..., 'FEAT', feature1, feature2, ...).join(' '))
    
    931 924
                 # NOTE: if there's a need to do more gzip, put that to a function
    
    932 925
                 try:
    
    933
    -                data = GzipFile(fileobj=BytesIO(data)).read().decode('utf-8').split('\0')
    
    926
    +                data = GzipFile(fileobj=BytesIO(row.data)).read().decode('utf-8').split('\0')
    
    934 927
                 except IOError:
    
    935 928
                     # This data is corrupted. It probably contains non-ascii chars
    
    936
    -                to_be_removed.append((hash_method, hash_))
    
    929
    +                to_be_removed.append((row.hash_method, row.hash))
    
    937 930
                     continue
    
    938 931
                 i = 0
    
    939 932
                 identities = list()
    
    ... ... @@ -952,7 +945,7 @@ class Logger:
    952 945
                     i += 1
    
    953 946
     
    
    954 947
                 # yield the row
    
    955
    -            yield hash_method, hash_, identities, features
    
    948
    +            yield row.hash_method, row.hash, identities, features
    
    956 949
             for hash_method, hash_ in to_be_removed:
    
    957 950
                 sql = '''DELETE FROM caps_cache WHERE hash_method = "%s" AND
    
    958 951
                         hash = "%s"''' % (hash_method, hash_)
    
    ... ... @@ -1091,9 +1084,10 @@ class Logger:
    1091 1084
                     SELECT j.jid, re.jid_id, re.name, re.subscription, re.ask
    
    1092 1085
                     FROM roster_entry re, jids j
    
    1093 1086
                     WHERE re.account_jid_id=? AND j.jid_id=re.jid_id''', (account_jid_id,))
    
    1094
    -        for jid, jid_id, name, subscription, ask in self.cur:
    
    1095
    -            jid = jid
    
    1096
    -            name = name
    
    1087
    +        for row in self.cur:
    
    1088
    +            #jid, jid_id, name, subscription, ask
    
    1089
    +            jid = row.jid
    
    1090
    +            name = row.name
    
    1097 1091
                 data[jid] = {}
    
    1098 1092
                 if name:
    
    1099 1093
                     data[jid]['name'] = name
    
    ... ... @@ -1101,14 +1095,14 @@ class Logger:
    1101 1095
                     data[jid]['name'] = None
    
    1102 1096
                 data[jid]['subscription'] = \
    
    1103 1097
                         self.convert_db_api_values_to_human_subscription_values(
    
    1104
    -                    subscription)
    
    1098
    +                    row.subscription)
    
    1105 1099
                 data[jid]['groups'] = []
    
    1106 1100
                 data[jid]['resources'] = {}
    
    1107
    -            if ask:
    
    1101
    +            if row.ask:
    
    1108 1102
                     data[jid]['ask'] = 'subscribe'
    
    1109 1103
                 else:
    
    1110 1104
                     data[jid]['ask'] = None
    
    1111
    -            data[jid]['id'] = jid_id
    
    1105
    +            data[jid]['id'] = row.jid_id
    
    1112 1106
     
    
    1113 1107
             # Then we add group for roster entries
    
    1114 1108
             for jid in data:
    
    ... ... @@ -1116,8 +1110,8 @@ class Logger:
    1116 1110
                         SELECT group_name FROM roster_group
    
    1117 1111
                         WHERE account_jid_id=? AND jid_id=?''',
    
    1118 1112
                         (account_jid_id, data[jid]['id']))
    
    1119
    -            for (group_name,) in self.cur:
    
    1120
    -                group_name = group_name
    
    1113
    +            for row in self.cur:
    
    1114
    +                group_name = row.group_name
    
    1121 1115
                     data[jid]['groups'].append(group_name)
    
    1122 1116
                 del data[jid]['id']
    
    1123 1117
     
    

  • gajim/roster_window.py
    ... ... @@ -1842,10 +1842,9 @@ class RosterWindow:
    1842 1842
             old unread messages, delete them from unread table
    
    1843 1843
             """
    
    1844 1844
             results = gajim.logger.get_unread_msgs()
    
    1845
    -        for result in results:
    
    1846
    -            jid = result[4]
    
    1847
    -            additional_data = result[5]
    
    1848
    -            shown = result[6]
    
    1845
    +        for result, shown in results:
    
    1846
    +            jid = result.jid
    
    1847
    +            additional_data = result.additional_data
    
    1849 1848
                 if gajim.contacts.get_first_contact_from_jid(account, jid) and not \
    
    1850 1849
                 shown:
    
    1851 1850
                     # We have this jid in our contacts list
    
    ... ... @@ -1853,17 +1852,17 @@ class RosterWindow:
    1853 1852
                     # with them
    
    1854 1853
                     session = gajim.connections[account].make_new_session(jid)
    
    1855 1854
     
    
    1856
    -                tim = float(result[2])
    
    1857
    -                session.roster_message(jid, result[1], tim, msg_type='chat',
    
    1858
    -                    msg_log_id=result[0], additional_data=additional_data)
    
    1859
    -                gajim.logger.set_shown_unread_msgs(result[0])
    
    1855
    +                tim = float(result.time)
    
    1856
    +                session.roster_message(jid, result.message, tim, msg_type='chat',
    
    1857
    +                    msg_log_id=result.log_line_id, additional_data=additional_data)
    
    1858
    +                gajim.logger.set_shown_unread_msgs(result.log_line_id)
    
    1860 1859
     
    
    1861
    -            elif (time.time() - result[2]) > 2592000:
    
    1860
    +            elif (time.time() - result.time) > 2592000:
    
    1862 1861
                     # ok, here we see that we have a message in unread messages
    
    1863 1862
                     # table that is older than a month. It is probably from someone
    
    1864 1863
                     # not in our roster for accounts we usually launch, so we will
    
    1865 1864
                     # delete this id from unread message tables.
    
    1866
    -                gajim.logger.set_read_messages([result[0]])
    
    1865
    +                gajim.logger.set_read_messages([result.log_line_id])
    
    1867 1866
     
    
    1868 1867
         def fill_contacts_and_groups_dicts(self, array, account):
    
    1869 1868
             """