Index: /pykota/trunk/pykota/storages/ldapstorage.py
===================================================================
--- /pykota/trunk/pykota/storages/ldapstorage.py (revision 2622)
+++ /pykota/trunk/pykota/storages/ldapstorage.py (revision 2652)
@@ -287,5 +287,5 @@
         raise PyKotaStorageError, message
             
-    def filterNames(self, records, attribute) :        
+    def filterNames(self, records, attribute, patterns=None) :
         """Returns a list of 'attribute' from a list of records.
         
@@ -297,55 +297,54 @@
             if attrval is None :
                 self.tool.printInfo("Object %s has no %s attribute !" % (dn, attribute), "error")
-            else :    
-                result.append(attrval)
+            else :
+                if patterns :
+                    if (not isinstance(patterns, type([]))) and (not isinstance(patterns, type(()))) :
+                        patterns = [ patterns ]
+                    patterns = [self.userCharsetToDatabase(p) for p in patterns]
+                    if self.tool.matchString(attrval, patterns) :   
+                        result.append(attrval)
+                else :    
+                    result.append(attrval)
         return result        
                 
     def getAllBillingCodes(self, billingcode=None) :    
         """Extracts all billing codes or only the billing codes matching the optional parameter."""
-        billingcodes = []
         ldapfilter = "objectClass=pykotaBilling"
-        if billingcode :
-            ldapfilter = "(&(%s)(pykotaBillingCode=%s))" % (ldapfilter, self.userCharsetToDatabase(billingcode))
         result = self.doSearch(ldapfilter, ["pykotaBillingCode"], base=self.info["billingcodebase"])
         if result :
-            billingcodes = [self.databaseToUserCharset(bc) for bc in self.filterNames(result, "pykotaBillingCode")]
-        return billingcodes
+            return [self.databaseToUserCharset(bc) for bc in self.filterNames(result, "pykotaBillingCode", billingcode)]
+        else :    
+            return []
         
     def getAllPrintersNames(self, printername=None) :    
         """Extracts all printer names or only the printers' names matching the optional parameter."""
-        printernames = []
         ldapfilter = "objectClass=pykotaPrinter"
-        if printername :
-            ldapfilter = "(&(%s)(pykotaPrinterName=%s))" % (ldapfilter, printername)
         result = self.doSearch(ldapfilter, ["pykotaPrinterName"], base=self.info["printerbase"])
         if result :
-            printernames = self.filterNames(result, "pykotaPrinterName")
-        return printernames
+            return self.filterNames(result, "pykotaPrinterName", printername)
+        else :    
+            return []
         
     def getAllUsersNames(self, username=None) :    
         """Extracts all user names or only the users' names matching the optional parameter."""
-        usernames = []
         ldapfilter = "objectClass=pykotaAccount"
-        if username :
-            ldapfilter = "(&(%s)(pykotaUserName=%s))" % (ldapfilter, username)
         result = self.doSearch(ldapfilter, ["pykotaUserName"], base=self.info["userbase"])
         if result :
-            usernames = self.filterNames(result, "pykotaUserName")
-        return usernames
+            return self.filterNames(result, "pykotaUserName", username)
+        else :    
+            return []
         
     def getAllGroupsNames(self, groupname=None) :    
         """Extracts all group names or only the groups' names matching the optional parameter."""
-        groupnames = []
         ldapfilter = "objectClass=pykotaGroup"
-        if groupname :
-            ldapfilter = "(&(%s)(pykotaGroupName=%s))" % (ldapfilter, groupname)
         result = self.doSearch(ldapfilter, ["pykotaGroupName"], base=self.info["groupbase"])
         if result :
-            groupnames = self.filterNames(result, "pykotaGroupName")
-        return groupnames
+            return self.filterNames(result, "pykotaGroupName", groupname)
+        else :    
+            return []
         
     def getUserNbJobsFromHistory(self, user) :
         """Returns the number of jobs the user has in history."""
-        result = self.doSearch("(&(pykotaUserName=%s)(objectClass=pykotaJob))" % user.Name, None, base=self.info["jobbase"])
+        result = self.doSearch("(&(pykotaUserName=%s)(objectClass=pykotaJob))" % self.userCharsetToDatabase(user.Name), None, base=self.info["jobbase"])
         return len(result)
         
@@ -353,9 +352,10 @@
         """Extracts user information given its name."""
         user = StorageUser(self, username)
+        username = self.userCharsetToDatabase(username)
         result = self.doSearch("(&(objectClass=pykotaAccount)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["userrdn"], username), ["pykotaUserName", "pykotaLimitBy", self.info["usermail"], "pykotaOverCharge"], base=self.info["userbase"])
         if result :
             fields = result[0][1]
             user.ident = result[0][0]
-            user.Name = fields.get("pykotaUserName", [username])[0] 
+            user.Name = fields.get("pykotaUserName", [self.databaseToUserCharset(username)])[0] 
             user.Email = fields.get(self.info["usermail"], [None])[0]
             user.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0]
@@ -398,9 +398,10 @@
         """Extracts group information given its name."""
         group = StorageGroup(self, groupname)
+        groupname = self.userCharsetToDatabase(groupname)
         result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (groupname, self.info["grouprdn"], groupname), ["pykotaGroupName", "pykotaLimitBy"], base=self.info["groupbase"])
         if result :
             fields = result[0][1]
             group.ident = result[0][0]
-            group.Name = fields.get("pykotaGroupName", [groupname])[0] 
+            group.Name = fields.get("pykotaGroupName", [self.databaseToUserCharset(groupname)])[0] 
             group.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0]
             group.AccountBalance = 0.0
@@ -416,4 +417,5 @@
         """Extracts printer information given its name : returns first matching printer."""
         printer = StoragePrinter(self, printername)
+        printername = self.userCharsetToDatabase(printername)
         result = self.doSearch("(&(objectClass=pykotaPrinter)(|(pykotaPrinterName=%s)(%s=%s)))" \
                       % (printername, self.info["printerrdn"], printername), \
@@ -425,5 +427,5 @@
             fields = result[0][1]       # take only first matching printer, ignore the rest
             printer.ident = result[0][0]
-            printer.Name = fields.get("pykotaPrinterName", [printername])[0] 
+            printer.Name = fields.get("pykotaPrinterName", [self.databaseToUserCharset(printername)])[0] 
             printer.PricePerJob = float(fields.get("pykotaPricePerJob", [0.0])[0])
             printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0])
@@ -447,5 +449,8 @@
             else :    
                 base = self.info["userquotabase"]
-            result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s)(pykotaPrinterName=%s))" % (user.Name, printer.Name), ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount"], base=base)
+            result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s)(pykotaPrinterName=%s))" % \
+                                      (self.userCharsetToDatabase(user.Name), self.userCharsetToDatabase(printer.Name)), \
+                                      ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount"], \
+                                      base=base)
             if result :
                 fields = result[0][1]
@@ -483,5 +488,8 @@
             else :    
                 base = self.info["groupquotabase"]
-            result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s)(pykotaPrinterName=%s))" % (group.Name, printer.Name), ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=base)
+            result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s)(pykotaPrinterName=%s))" % \
+                                      (self.userCharsetToDatabase(group.Name), self.userCharsetToDatabase(printer.Name)), \
+                                      ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], \
+                                      base=base)
             if result :
                 fields = result[0][1]
@@ -507,5 +515,5 @@
                 grouppquota.PageCounter = 0
                 grouppquota.LifePageCounter = 0
-                usernamesfilter = "".join(["(pykotaUserName=%s)" % member.Name for member in self.getGroupMembers(group)])
+                usernamesfilter = "".join(["(pykotaUserName=%s)" % self.userCharsetToDatabase(member.Name) for member in self.getGroupMembers(group)])
                 if usernamesfilter :
                     usernamesfilter = "(|%s)" % usernamesfilter
@@ -514,5 +522,7 @@
                 else :
                     base = self.info["userquotabase"]
-                result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)%s)" % (printer.Name, usernamesfilter), ["pykotaPageCounter", "pykotaLifePageCounter"], base=base)
+                result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)%s)" % \
+                                          (self.userCharsetToDatabase(printer.Name), usernamesfilter), \
+                                          ["pykotaPageCounter", "pykotaLifePageCounter"], base=base)
                 if result :
                     for userpquota in result :    
@@ -525,5 +535,9 @@
         """Extracts a printer's last job information."""
         lastjob = StorageLastJob(self, printer)
-        result = self.doSearch("(&(objectClass=pykotaLastjob)(|(pykotaPrinterName=%s)(%s=%s)))" % (printer.Name, self.info["printerrdn"], printer.Name), ["pykotaLastJobIdent"], base=self.info["lastjobbase"])
+        pname = self.userCharsetToDatabase(printer.Name)
+        result = self.doSearch("(&(objectClass=pykotaLastjob)(|(pykotaPrinterName=%s)(%s=%s)))" % \
+                                  (pname, self.info["printerrdn"], pname), \
+                                  ["pykotaLastJobIdent"], \
+                                  base=self.info["lastjobbase"])
         if result :
             lastjob.lastjobident = result[0][0]
@@ -557,5 +571,5 @@
                 lastjob.ident = result[0][0]
                 lastjob.JobId = fields.get("pykotaJobId")[0]
-                lastjob.UserName = fields.get("pykotaUserName")[0]
+                lastjob.UserName = self.databaseToUserCharset(fields.get("pykotaUserName")[0])
                 lastjob.PrinterPageCounter = int(fields.get("pykotaPrinterPageCounter", [0])[0])
                 try :
@@ -596,8 +610,12 @@
         """Returns the group's members list."""
         groupmembers = []
-        result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (group.Name, self.info["grouprdn"], group.Name), [self.info["groupmembers"]], base=self.info["groupbase"])
+        gname = self.userCharsetToDatabase(group.Name)
+        result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % \
+                                  (gname, self.info["grouprdn"], gname), \
+                                  [self.info["groupmembers"]], \
+                                  base=self.info["groupbase"])
         if result :
             for username in result[0][1].get(self.info["groupmembers"], []) :
-                groupmembers.append(self.getUser(username))
+                groupmembers.append(self.getUser(self.databaseToUserCharset(username)))
         return groupmembers        
         
@@ -605,8 +623,12 @@
         """Returns the user's groups list."""
         groups = []
-        result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % (self.info["groupmembers"], user.Name), [self.info["grouprdn"], "pykotaGroupName", "pykotaLimitBy"], base=self.info["groupbase"])
+        uname = self.userCharsetToDatabase(user.Name)
+        result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % \
+                                  (self.info["groupmembers"], uname), \
+                                  [self.info["grouprdn"], "pykotaGroupName", "pykotaLimitBy"], \
+                                  base=self.info["groupbase"])
         if result :
             for (groupid, fields) in result :
-                groupname = (fields.get("pykotaGroupName", [None]) or fields.get(self.info["grouprdn"], [None]))[0]
+                groupname = self.databaseToUserCharset((fields.get("pykotaGroupName", [None]) or fields.get(self.info["grouprdn"], [None]))[0])
                 group = self.getFromCache("GROUPS", groupname)
                 if group is None :
@@ -632,9 +654,12 @@
         """Get all the printer groups this printer is a member of."""
         pgroups = []
-        result = self.doSearch("(&(objectClass=pykotaPrinter)(uniqueMember=%s))" % printer.ident, ["pykotaPrinterName"], base=self.info["printerbase"])
+        result = self.doSearch("(&(objectClass=pykotaPrinter)(uniqueMember=%s))" % \
+                                  printer.ident, \
+                                  ["pykotaPrinterName"], \
+                                  base=self.info["printerbase"])
         if result :
             for (printerid, fields) in result :
                 if printerid != printer.ident : # In case of integrity violation.
-                    parentprinter = self.getPrinter(fields.get("pykotaPrinterName")[0])
+                    parentprinter = self.getPrinter(self.databaseToUserCharset(fields.get("pykotaPrinterName")[0]))
                     if parentprinter.Exists :
                         pgroups.append(parentprinter)
@@ -645,8 +670,12 @@
         printers = []
         # see comment at the same place in pgstorage.py
-        result = self.doSearch("(&(objectClass=pykotaPrinter)(|%s))" % "".join(["(pykotaPrinterName=%s)(%s=%s)" % (pname, self.info["printerrdn"], pname) for pname in printerpattern.split(",")]), ["pykotaPrinterName", "pykotaPricePerPage", "pykotaPricePerJob", "pykotaMaxJobSize", "pykotaPassThrough", "uniqueMember", "description"], base=self.info["printerbase"])
+        printerpattern = [self.userCharsetToDatabase(p) for p in printerpattern.split(",")]
+        result = self.doSearch("(&(objectClass=pykotaPrinter)(|%s))" % \
+                                  "".join(["(pykotaPrinterName=%s)(%s=%s)" % (pname, self.info["printerrdn"], pname) for pname in printerpattern]), \
+                                  ["pykotaPrinterName", "pykotaPricePerPage", "pykotaPricePerJob", "pykotaMaxJobSize", "pykotaPassThrough", "uniqueMember", "description"], \
+                                  base=self.info["printerbase"])
         if result :
             for (printerid, fields) in result :
-                printername = fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0]
+                printername = self.databaseToUserCharset(fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0])
                 printer = StoragePrinter(self, printername)
                 printer.ident = printerid
@@ -669,12 +698,17 @@
         """Returns the list of users who uses a given printer, along with their quotas."""
         usersandquotas = []
+        pname = self.userCharsetToDatabase(printer.Name)
+        names = [self.userCharsetToDatabase(n) for n in names]
         if self.info["userquotabase"].lower() == "user" :
            base = self.info["userbase"]
         else :
            base = self.info["userquotabase"]
-        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)(|%s))" % (printer.Name, "".join(["(pykotaUserName=%s)" % uname for uname in names])), ["pykotaUserName", "pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount"], base=base)
+        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)(|%s))" % \
+                                  (pname, "".join(["(pykotaUserName=%s)" % uname for uname in names])), \
+                                  ["pykotaUserName", "pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount"], \
+                                  base=base)
         if result :
             for (userquotaid, fields) in result :
-                user = self.getUser(fields.get("pykotaUserName")[0])
+                user = self.getUser(self.databaseToUserCharset(fields.get("pykotaUserName")[0]))
                 userpquota = StorageUserPQuota(self, user, printer)
                 userpquota.ident = userquotaid
@@ -709,12 +743,17 @@
         """Returns the list of groups which uses a given printer, along with their quotas."""
         groupsandquotas = []
+        pname = self.userCharsetToDatabase(printer.Name)
+        names = [self.userCharsetToDatabase(n) for n in names]
         if self.info["groupquotabase"].lower() == "group" :
            base = self.info["groupbase"]
         else :
            base = self.info["groupquotabase"]
-        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s)(|%s))" % (printer.Name, "".join(["(pykotaGroupName=%s)" % gname for gname in names])), ["pykotaGroupName"], base=base)
+        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s)(|%s))" % \
+                                  (pname, "".join(["(pykotaGroupName=%s)" % gname for gname in names])), \
+                                  ["pykotaGroupName"], \
+                                  base=base)
         if result :
             for (groupquotaid, fields) in result :
-                group = self.getGroup(fields.get("pykotaGroupName")[0])
+                group = self.getGroup(self.databaseToUserCharset(fields.get("pykotaGroupName")[0]))
                 grouppquota = self.getGroupPQuota(group, printer)
                 groupsandquotas.append((group, grouppquota))
@@ -724,4 +763,5 @@
     def addPrinter(self, printername) :        
         """Adds a printer to the quota storage, returns it."""
+        printername = self.userCharsetToDatabase(printername)
         fields = { self.info["printerrdn"] : printername,
                    "objectClass" : ["pykotaObject", "pykotaPrinter"],
@@ -739,6 +779,7 @@
     def addUser(self, user) :        
         """Adds a user to the quota storage, returns it."""
+        uname = self.userCharsetToDatabase(user.Name)
         newfields = {
-                       "pykotaUserName" : user.Name,
+                       "pykotaUserName" : uname,
                        "pykotaLimitBy" : (user.LimitBy or "quota"),
                        "pykotaOverCharge" : str(user.OverCharge),
@@ -753,5 +794,8 @@
             except ValueError :
                 (where, action) = (self.info["newuser"].strip(), "fail")
-            result = self.doSearch("(&(objectClass=%s)(%s=%s))" % (where, self.info["userrdn"], user.Name), None, base=self.info["userbase"])
+            result = self.doSearch("(&(objectClass=%s)(%s=%s))" % \
+                                      (where, self.info["userrdn"], uname), \
+                                      None, \
+                                      base=self.info["userbase"])
             if result :
                 (dn, fields) = result[0]
@@ -772,26 +816,26 @@
         if mustadd :
             if self.info["userbase"] == self.info["balancebase"] :            
-                fields = { self.info["userrdn"] : user.Name,
+                fields = { self.info["userrdn"] : uname,
                            "objectClass" : ["pykotaObject", "pykotaAccount", "pykotaAccountBalance"],
-                           "cn" : user.Name,
+                           "cn" : uname,
                            "pykotaBalance" : str(user.AccountBalance or 0.0),
                            "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0), 
                          } 
             else :             
-                fields = { self.info["userrdn"] : user.Name,
+                fields = { self.info["userrdn"] : uname,
                            "objectClass" : ["pykotaObject", "pykotaAccount"],
-                           "cn" : user.Name,
+                           "cn" : uname,
                          } 
             fields.update(newfields)         
-            dn = "%s=%s,%s" % (self.info["userrdn"], user.Name, self.info["userbase"])
+            dn = "%s=%s,%s" % (self.info["userrdn"], uname, self.info["userbase"])
             self.doAdd(dn, fields)
             if self.info["userbase"] != self.info["balancebase"] :            
-                fields = { self.info["balancerdn"] : user.Name,
+                fields = { self.info["balancerdn"] : uname,
                            "objectClass" : ["pykotaObject", "pykotaAccountBalance"],
-                           "cn" : user.Name,
+                           "cn" : uname,
                            "pykotaBalance" : str(user.AccountBalance or 0.0),
                            "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0),  
                          } 
-                dn = "%s=%s,%s" % (self.info["balancerdn"], user.Name, self.info["balancebase"])
+                dn = "%s=%s,%s" % (self.info["balancerdn"], uname, self.info["balancebase"])
                 self.doAdd(dn, fields)
             
@@ -800,6 +844,7 @@
     def addGroup(self, group) :        
         """Adds a group to the quota storage, returns it."""
+        gname = self.userCharsetToDatabase(group.Name)
         newfields = { 
-                      "pykotaGroupName" : group.Name,
+                      "pykotaGroupName" : gname,
                       "pykotaLimitBy" : (group.LimitBy or "quota"),
                     } 
@@ -810,5 +855,8 @@
             except ValueError :
                 (where, action) = (self.info["newgroup"].strip(), "fail")
-            result = self.doSearch("(&(objectClass=%s)(%s=%s))" % (where, self.info["grouprdn"], group.Name), None, base=self.info["groupbase"])
+            result = self.doSearch("(&(objectClass=%s)(%s=%s))" % \
+                                      (where, self.info["grouprdn"], gname), \
+                                      None, \
+                                      base=self.info["groupbase"])
             if result :
                 (dn, fields) = result[0]
@@ -826,10 +874,10 @@
                 
         if mustadd :
-            fields = { self.info["grouprdn"] : group.Name,
+            fields = { self.info["grouprdn"] : gname,
                        "objectClass" : ["pykotaObject", "pykotaGroup"],
-                       "cn" : group.Name,
+                       "cn" : gname,
                      } 
             fields.update(newfields)         
-            dn = "%s=%s,%s" % (self.info["grouprdn"], group.Name, self.info["groupbase"])
+            dn = "%s=%s,%s" % (self.info["grouprdn"], gname, self.info["groupbase"])
             self.doAdd(dn, fields)
         return self.getGroup(group.Name)
@@ -843,5 +891,5 @@
                 if not fields.has_key(self.info["groupmembers"]) :
                     fields[self.info["groupmembers"]] = []
-                fields[self.info["groupmembers"]].append(user.Name)
+                fields[self.info["groupmembers"]].append(self.userCharsetToDatabase(user.Name))
                 self.doModify(group.ident, fields)
                 group.Members.append(user)
@@ -850,8 +898,10 @@
         """Initializes a user print quota on a printer."""
         uuid = self.genUUID()
+        uname = self.userCharsetToDatabase(user.Name)
+        pname = self.userCharsetToDatabase(printer.Name)
         fields = { "cn" : uuid,
                    "objectClass" : ["pykotaObject", "pykotaUserPQuota"],
-                   "pykotaUserName" : user.Name,
-                   "pykotaPrinterName" : printer.Name,
+                   "pykotaUserName" : uname,
+                   "pykotaPrinterName" : pname,
                    "pykotaDateLimit" : "None",
                    "pykotaPageCounter" : "0",
@@ -869,8 +919,10 @@
         """Initializes a group print quota on a printer."""
         uuid = self.genUUID()
+        gname = self.userCharsetToDatabase(group.Name)
+        pname = self.userCharsetToDatabase(printer.Name)
         fields = { "cn" : uuid,
                    "objectClass" : ["pykotaObject", "pykotaGroupPQuota"],
-                   "pykotaGroupName" : group.Name,
-                   "pykotaPrinterName" : printer.Name,
+                   "pykotaGroupName" : gname,
+                   "pykotaPrinterName" : pname,
                    "pykotaDateLimit" : "None",
                  } 
@@ -1002,4 +1054,6 @@
     def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None, jobmd5sum=None, jobpages=None, jobbilling=None, precomputedsize=None, precomputedprice=None) :
         """Adds a job in a printer's history."""
+        uname = self.userCharsetToDatabase(user.Name)
+        pname = self.userCharsetToDatabase(printer.Name)
         if (not self.disablehistory) or (not printer.LastJob.Exists) :
             uuid = self.genUUID()
@@ -1014,6 +1068,6 @@
                    "objectClass" : ["pykotaObject", "pykotaJob"],
                    "cn" : uuid,
-                   "pykotaUserName" : user.Name,
-                   "pykotaPrinterName" : printer.Name,
+                   "pykotaUserName" : uname,
+                   "pykotaPrinterName" : pname,
                    "pykotaJobId" : jobid,
                    "pykotaPrinterPageCounter" : str(pagecounter),
@@ -1051,5 +1105,5 @@
                        "objectClass" : ["pykotaObject", "pykotaLastJob"],
                        "cn" : lastjuuid,
-                       "pykotaPrinterName" : printer.Name,
+                       "pykotaPrinterName" : pname,
                        "pykotaLastJobIdent" : uuid,
                      }  
@@ -1115,7 +1169,7 @@
         where = []
         if user is not None :
-            where.append("(pykotaUserName=%s)" % user.Name)
+            where.append("(pykotaUserName=%s)" % self.userCharsetToDatabase(user.Name))
         if printer is not None :
-            where.append("(pykotaPrinterName=%s)" % printer.Name)
+            where.append("(pykotaPrinterName=%s)" % self.userCharsetToDatabase(printer.Name))
         if hostname is not None :
             where.append("(pykotaHostName=%s)" % hostname)
@@ -1188,6 +1242,6 @@
                    ((end is None) and (job.JobDate >= start)) or \
                    ((job.JobDate >= start) and (job.JobDate <= end)) :
-                    job.UserName = fields.get("pykotaUserName")[0]
-                    job.PrinterName = fields.get("pykotaPrinterName")[0]
+                    job.UserName = self.databaseToUserCharset(fields.get("pykotaUserName")[0])
+                    job.PrinterName = self.databaseToUserCharset(fields.get("pykotaPrinterName")[0])
                     job.Exists = 1
                     jobs.append(job)
@@ -1199,6 +1253,7 @@
     def deleteUser(self, user) :    
         """Completely deletes an user from the Quota Storage."""
+        uname = self.userCharsetToDatabase(user.Name)
         todelete = []    
-        result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s))" % user.Name, base=self.info["jobbase"])
+        result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s))" % uname, base=self.info["jobbase"])
         for (ident, fields) in result :
             todelete.append(ident)
@@ -1207,5 +1262,7 @@
         else :
             base = self.info["userquotabase"]
-        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s))" % user.Name, ["pykotaPrinterName", "pykotaUserName"], base=base)
+        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s))" % uname, \
+                                  ["pykotaPrinterName", "pykotaUserName"], \
+                                  base=base)
         for (ident, fields) in result :
             # ensure the user print quota entry will be deleted
@@ -1214,6 +1271,5 @@
             # if last job of current printer was printed by the user
             # to delete, we also need to delete the printer's last job entry.
-            printername = fields["pykotaPrinterName"][0]
-            printer = self.getPrinter(printername)
+            printer = self.getPrinter(self.databaseToUserCharset(fields["pykotaPrinterName"][0]))
             if printer.LastJob.UserName == user.Name :
                 todelete.append(printer.LastJob.lastjobident)
@@ -1241,5 +1297,8 @@
             else :    
                 self.doDelete(user.ident)
-        result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % user.Name, ["pykotaUserName"], base=self.info["balancebase"])
+        result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % \
+                                   uname, \
+                                   ["pykotaUserName"], \
+                                   base=self.info["balancebase"])
         for (ident, fields) in result :
             self.doDelete(ident)
@@ -1247,9 +1306,13 @@
     def deleteGroup(self, group) :    
         """Completely deletes a group from the Quota Storage."""
+        gname = self.userCharsetToDatabase(group.Name)
         if self.info["groupquotabase"].lower() == "group" :
             base = self.info["groupbase"]
         else :
             base = self.info["groupquotabase"]
-        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s))" % group.Name, ["pykotaGroupName"], base=base)
+        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s))" % \
+                                  gname, \
+                                  ["pykotaGroupName"], \
+                                  base=base)
         for (ident, fields) in result :
             self.doDelete(ident)
@@ -1276,8 +1339,9 @@
     def deletePrinter(self, printer) :    
         """Completely deletes a printer from the Quota Storage."""
-        result = self.doSearch("(&(objectClass=pykotaLastJob)(pykotaPrinterName=%s))" % printer.Name, base=self.info["lastjobbase"])
+        pname = self.userCharsetToDatabase(printer.Name)
+        result = self.doSearch("(&(objectClass=pykotaLastJob)(pykotaPrinterName=%s))" % pname, base=self.info["lastjobbase"])
         for (ident, fields) in result :
             self.doDelete(ident)
-        result = self.doSearch("(&(objectClass=pykotaJob)(pykotaPrinterName=%s))" % printer.Name, base=self.info["jobbase"])
+        result = self.doSearch("(&(objectClass=pykotaJob)(pykotaPrinterName=%s))" % pname, base=self.info["jobbase"])
         for (ident, fields) in result :
             self.doDelete(ident)
@@ -1286,5 +1350,5 @@
         else :
             base = self.info["groupquotabase"]
-        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s))" % printer.Name, base=base)
+        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s))" % pname, base=base)
         for (ident, fields) in result :
             self.doDelete(ident)
@@ -1293,5 +1357,5 @@
         else :
             base = self.info["userquotabase"]
-        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s))" % printer.Name, base=base)
+        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s))" % pname, base=base)
         for (ident, fields) in result :
             self.doDelete(ident)
@@ -1443,5 +1507,8 @@
         code = StorageBillingCode(self, label)
         ulabel = self.userCharsetToDatabase(label)
-        result = self.doSearch("(&(objectClass=pykotaBilling)(pykotaBillingCode=%s))" % ulabel, ["pykotaBillingCode", "pykotaBalance", "pykotaPageCounter", "description"], base=self.info["billingcodebase"])
+        result = self.doSearch("(&(objectClass=pykotaBilling)(pykotaBillingCode=%s))" % \
+                                  ulabel, \
+                                  ["pykotaBillingCode", "pykotaBalance", "pykotaPageCounter", "description"], \
+                                  base=self.info["billingcodebase"])
         if result :
             fields = result[0][1]       # take only first matching code, ignore the rest
@@ -1478,5 +1545,7 @@
         """Returns the list of all billing codes which match a certain pattern."""
         codes = []
-        result = self.doSearch("(&(objectClass=pykotaBilling)(|%s))" % "".join(["(pykotaBillingCode=%s)" % self.userCharsetToDatabase(bcode) for bcode in billingcodepattern.split(",")]), \
+        billingcodepattern = [self.userCharsetToDatabase(b) for b in billingcodepattern.split(",")]
+        result = self.doSearch("(&(objectClass=pykotaBilling)(|%s))" % \
+                                "".join(["(pykotaBillingCode=%s)" % bcode for bcode in billingcodepattern]), \
                                 ["pykotaBillingCode", "description", "pykotaPageCounter", "pykotaBalance"], \
                                 base=self.info["billingcodebase"])
