| 1 | """ |
|---|
| 2 | """ |
|---|
| 3 | import os, os.path, traceback, sqlite3 |
|---|
| 4 | |
|---|
| 5 | import wx, wx.xrc |
|---|
| 6 | |
|---|
| 7 | from wxHelper import GUI_ID, wxKeyFunctionSink, XrcControls, \ |
|---|
| 8 | runDialogModalFactory |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | from .OsAbstract import normalizePath, deleteFile |
|---|
| 15 | from . import StringOps, WindowLayout |
|---|
| 16 | |
|---|
| 17 | from EnhancedGrid import EnhancedGrid |
|---|
| 18 | |
|---|
| 19 | from .ConnectWrapPysqlite import ConnectWrapSyncCommit |
|---|
| 20 | from .DocPages import AliasWikiPage |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class InfoDatabase: |
|---|
| 25 | def __init__(self, mainControl): |
|---|
| 26 | self.tempDb = None |
|---|
| 27 | self.mainControl = mainControl |
|---|
| 28 | self.wikiDocument = mainControl.getWikiDocument() |
|---|
| 29 | |
|---|
| 30 | self.fileStorDir = self.wikiDocument.getFileStorage().getStoragePath() |
|---|
| 31 | self.normFileStorDir = normalizePath(self.fileStorDir) |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | def _createDatabase(self): |
|---|
| 35 | """ |
|---|
| 36 | Create empty database with needed scheme. |
|---|
| 37 | """ |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | tempDb = ConnectWrapSyncCommit(sqlite3.connect(ur"")) |
|---|
| 41 | |
|---|
| 42 | tempDb.execSql("create table fStorItems(" |
|---|
| 43 | "id integer primary key not null, " |
|---|
| 44 | "procCounter integer not null, " |
|---|
| 45 | "fullpath text not null default '', " |
|---|
| 46 | "normpath text not null default '', " |
|---|
| 47 | "relpath text not null default '', " |
|---|
| 48 | "type integer not null default 0, " |
|---|
| 49 | "deepness integer not null, " |
|---|
| 50 | "containerId integer not null default -1, " |
|---|
| 51 | "directref integer not null default 0," |
|---|
| 52 | "downwardref integer not null default 0," |
|---|
| 53 | "upwardref integer not null default 0," |
|---|
| 54 | "action integer not null default 0," |
|---|
| 55 | "isdefaultaction integer not null default 0," |
|---|
| 56 | "calcaction integer not null default 0, " |
|---|
| 57 | "errormsg text not null default '' " |
|---|
| 58 | |
|---|
| 59 | ");") |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | tempDb.execSql("create table refedItems(" |
|---|
| 64 | "id integer primary key not null, " |
|---|
| 65 | "fullpath text not null default ''," |
|---|
| 66 | "normpath text not null default ''," |
|---|
| 67 | "infstore integer not null default 0," |
|---|
| 68 | "present integer not null default 0," |
|---|
| 69 | "action integer not null default 0" |
|---|
| 70 | |
|---|
| 71 | ");") |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | tempDb.execSql("create table unifNameToItem(" |
|---|
| 76 | "unifName text not null default ''," |
|---|
| 77 | "presentationUrl text not null default ''," |
|---|
| 78 | "refedItemId integer not null default 0," |
|---|
| 79 | "relative integer not null default 0," |
|---|
| 80 | "tokenPos integer not null default -1," |
|---|
| 81 | "tokenLength integer not null default -1," |
|---|
| 82 | "corePos integer not null default -1," |
|---|
| 83 | |
|---|
| 84 | "coreLength integer not null default -1" |
|---|
| 85 | ");") |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | tempDb.commit() |
|---|
| 91 | |
|---|
| 92 | self.tempDb = tempDb |
|---|
| 93 | |
|---|
| 94 | def getSqlDb(self): |
|---|
| 95 | return self.tempDb |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | def _scanFileStore(self): |
|---|
| 99 | assert self.tempDb |
|---|
| 100 | |
|---|
| 101 | if not os.path.isdir(self.fileStorDir): |
|---|
| 102 | |
|---|
| 103 | return |
|---|
| 104 | |
|---|
| 105 | self.tempDb.execSql("insert into fStorItems(procCounter, fullpath, " |
|---|
| 106 | "normpath, relpath, type, containerId, deepness) " |
|---|
| 107 | "values(0, ?, ?, '', 1, -1, 0)", |
|---|
| 108 | (self.fileStorDir, self.normFileStorDir)) |
|---|
| 109 | procCounter = 1 |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | dirStack = [(self.fileStorDir, self.tempDb.lastrowid, u"", 0)] |
|---|
| 113 | |
|---|
| 114 | while dirStack: |
|---|
| 115 | procDir, procDirId, relPath, dirDeepness = dirStack.pop() |
|---|
| 116 | |
|---|
| 117 | items = os.listdir(procDir) |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | for fn in items: |
|---|
| 121 | fpi = os.path.join(procDir, fn) |
|---|
| 122 | relFpi = os.path.join(relPath, fn) |
|---|
| 123 | |
|---|
| 124 | if os.path.isfile(StringOps.longPathEnc(fpi)): |
|---|
| 125 | self.tempDb.execSql("insert into fStorItems(procCounter, " |
|---|
| 126 | "fullpath, normpath, relpath, type, containerId, " |
|---|
| 127 | "deepness) values(?, ?, ?, ?, 0, ?, ?)", |
|---|
| 128 | (procCounter, fpi, normalizePath(fpi), relFpi, |
|---|
| 129 | procDirId, dirDeepness + 1)) |
|---|
| 130 | procCounter += 1 |
|---|
| 131 | elif os.path.isdir(StringOps.longPathEnc(fpi)) \ |
|---|
| 132 | and not os.path.islink(StringOps.longPathEnc(fpi)): |
|---|
| 133 | self.tempDb.execSql("insert into fStorItems(procCounter, " |
|---|
| 134 | "fullpath, normpath, relpath, type, containerId, " |
|---|
| 135 | "deepness) values(?, ?, ?, ?, 1, ?, ?)", |
|---|
| 136 | (procCounter, fpi, normalizePath(fpi), relFpi, |
|---|
| 137 | procDirId, dirDeepness + 1)) |
|---|
| 138 | procCounter += 1 |
|---|
| 139 | dirStack.append((fpi, self.tempDb.lastrowid, relFpi, |
|---|
| 140 | dirDeepness + 1)) |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | def updateWikiPage(self, wikiPage): |
|---|
| 144 | pageAst = wikiPage.getLivePageAst() |
|---|
| 145 | |
|---|
| 146 | self.tempDb.execSql("delete from unifNameToItem where " |
|---|
| 147 | "unifName == ?", (wikiPage.getUnifiedPageName(),)) |
|---|
| 148 | |
|---|
| 149 | for urlNode in pageAst.iterDeepByName("urlLink"): |
|---|
| 150 | url = urlNode.url |
|---|
| 151 | |
|---|
| 152 | if url.startswith(u"rel://"): |
|---|
| 153 | url = self.wikiDocument.makeRelUrlAbsolute(url) |
|---|
| 154 | isRel = 1 |
|---|
| 155 | else: |
|---|
| 156 | isRel = 0 |
|---|
| 157 | |
|---|
| 158 | if url.startswith(u"file:"): |
|---|
| 159 | path = StringOps.pathnameFromUrl(url) |
|---|
| 160 | npath = normalizePath(path) |
|---|
| 161 | pathId = self.tempDb.execSqlQuerySingleItem( |
|---|
| 162 | "select id from refedItems where normpath=?", |
|---|
| 163 | (npath,)) |
|---|
| 164 | if pathId is None: |
|---|
| 165 | |
|---|
| 166 | lpe = StringOps.longPathEnc(path) |
|---|
| 167 | pathEx = os.path.isfile(lpe) \ |
|---|
| 168 | or (os.path.isdir(lpe) |
|---|
| 169 | and not os.path.islink(lpe)) |
|---|
| 170 | pathEx = 1 if pathEx else 0 |
|---|
| 171 | |
|---|
| 172 | inFileStor = self.normFileStorDir == npath or \ |
|---|
| 173 | StringOps.testContainedInDir(self.normFileStorDir, |
|---|
| 174 | npath) |
|---|
| 175 | inFileStor = 1 if inFileStor else 0 |
|---|
| 176 | |
|---|
| 177 | self.tempDb.execSql("insert into refedItems(" |
|---|
| 178 | "fullpath, normpath, infstore, present) " |
|---|
| 179 | "values(?, ?, ?, ?)", |
|---|
| 180 | (path, npath, inFileStor, pathEx)) |
|---|
| 181 | |
|---|
| 182 | pathId = self.tempDb.lastrowid |
|---|
| 183 | |
|---|
| 184 | self.tempDb.execSql("insert or replace into unifNameToItem(" |
|---|
| 185 | "unifName, presentationUrl, refedItemId, relative," |
|---|
| 186 | "tokenPos, tokenLength, corePos, coreLength) " |
|---|
| 187 | "values (?, ?, ?, ?, ?, ?, ?, ?)", |
|---|
| 188 | (wikiPage.getUnifiedPageName(), |
|---|
| 189 | urlNode.coreNode.getString(), pathId, isRel, |
|---|
| 190 | urlNode.pos, urlNode.strLength, |
|---|
| 191 | urlNode.coreNode.pos, |
|---|
| 192 | urlNode.coreNode.strLength)) |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | def _scanLinks(self, progresshandler): |
|---|
| 196 | wikiWords = self.wikiDocument.getWikiData().getAllDefinedWikiPageNames() |
|---|
| 197 | |
|---|
| 198 | progresshandler.open(len(wikiWords) + 1) |
|---|
| 199 | try: |
|---|
| 200 | step = 1 |
|---|
| 201 | |
|---|
| 202 | for wikiWord in wikiWords: |
|---|
| 203 | progresshandler.update(step, _(u"Scan links in %s") % wikiWord) |
|---|
| 204 | |
|---|
| 205 | wikiPage = self.wikiDocument._getWikiPageNoErrorNoCache(wikiWord) |
|---|
| 206 | if isinstance(wikiPage, AliasWikiPage): |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | continue |
|---|
| 211 | |
|---|
| 212 | self.updateWikiPage(wikiPage) |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 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 | step += 1 |
|---|
| 261 | |
|---|
| 262 | finally: |
|---|
| 263 | progresshandler.close() |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | def _markDirectlyReferencedInFileStorage(self): |
|---|
| 267 | """ |
|---|
| 268 | If a path is present in refedItems and fStorItems then |
|---|
| 269 | fStorItems.directref should be set 1 |
|---|
| 270 | """ |
|---|
| 271 | |
|---|
| 272 | self.tempDb.execSql("update fStorItems set directref = 1 where " |
|---|
| 273 | "normpath in (select normpath from refedItems)") |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | def _inferUpwardRefInFileStorage(self): |
|---|
| 277 | """ |
|---|
| 278 | If an item has a direct or upward reference then its containing directory |
|---|
| 279 | should also have an upward reference i.e. if a file is referenced its |
|---|
| 280 | containing directory must be kept, too. |
|---|
| 281 | """ |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | self.tempDb.execSqlUntilNoChange("update fStorItems set upwardref = 1 where " |
|---|
| 286 | "upwardref == 0 and id in (select containerId from fStorItems " |
|---|
| 287 | "where directref != 0 or upwardref != 0)") |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | def _inferDownwardRefInFileStorage(self): |
|---|
| 291 | """ |
|---|
| 292 | If a directory has a direct or downward reference then its contained |
|---|
| 293 | items also get a downward reference. The idea is that if the user |
|---|
| 294 | references a particular directory she probably also wants to keep |
|---|
| 295 | the files and directories in it even if they aren't referenced. |
|---|
| 296 | |
|---|
| 297 | This is optional. Maybe there will be an option later to |
|---|
| 298 | run this only to a given deepness. |
|---|
| 299 | """ |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | self.tempDb.execSqlUntilNoChange("update fStorItems set downwardref = 1 where " |
|---|
| 304 | "downwardref == 0 and containerId in " |
|---|
| 305 | "(select id from fStorItems where " |
|---|
| 306 | "type == 1 and (directref != 0 or downwardref != 0))") |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | def deleteUninteresting(self): |
|---|
| 310 | """ |
|---|
| 311 | Remove all rows for items which exist and are referenced. |
|---|
| 312 | """ |
|---|
| 313 | self.tempDb.execSql("delete from fStorItems where " |
|---|
| 314 | "(directref or downwardref or upwardref)") |
|---|
| 315 | self.tempDb.execSql("delete from refedItems where present") |
|---|
| 316 | self.tempDb.execSql("delete from unifNameToItem where " |
|---|
| 317 | "refedItemId not in (select id from refedItems)") |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | def buildDatabaseBeforeDialog(self, progresshandler, options): |
|---|
| 321 | self._createDatabase() |
|---|
| 322 | self._scanFileStore() |
|---|
| 323 | self._scanLinks(progresshandler) |
|---|
| 324 | self._markDirectlyReferencedInFileStorage() |
|---|
| 325 | self._inferUpwardRefInFileStorage() |
|---|
| 326 | if options["downwardRef"]: |
|---|
| 327 | self._inferDownwardRefInFileStorage() |
|---|
| 328 | self.deleteUninteresting() |
|---|
| 329 | |
|---|
| 330 | self.tempDb.commit() |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | def calcActionAndErrorsDuringDialog(self, orphanedActionDefault, |
|---|
| 334 | orphanedDownwardRef): |
|---|
| 335 | |
|---|
| 336 | CALCACTION_KEEP = 1 |
|---|
| 337 | CALCACTION_DELETE = 2 |
|---|
| 338 | CALCACTION_COLLECT = 5 |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | MASK_KEEP = 1 |
|---|
| 342 | MASK_COLLECT = 4 |
|---|
| 343 | |
|---|
| 344 | MASK_INFER_UPWARD = 16 |
|---|
| 345 | MASK_INFER_DOWNWARD = 32 |
|---|
| 346 | |
|---|
| 347 | error = False |
|---|
| 348 | |
|---|
| 349 | self.tempDb.commit() |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | self.tempDb.execSql(""" |
|---|
| 358 | update fStorItems set calcaction = case |
|---|
| 359 | when action == 3 then 5 |
|---|
| 360 | else action |
|---|
| 361 | end, |
|---|
| 362 | errormsg=''""") |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | if orphanedActionDefault == 3: |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | self.tempDb.execSql("update fStorItems set calcaction=4 " |
|---|
| 371 | "where calcaction==0") |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | self.tempDb.execSqlUntilNoChange("update fStorItems set calcaction=calcaction|1|16 " |
|---|
| 381 | "where (calcaction & 1) == 0 and id in (select containerId from fStorItems " |
|---|
| 382 | "where (calcaction & 1) == 1)") |
|---|
| 383 | |
|---|
| 384 | if orphanedDownwardRef: |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | self.tempDb.execSqlUntilNoChange("update fStorItems " |
|---|
| 388 | "set calcaction=calcaction|1|32 " |
|---|
| 389 | "where (calcaction & 3) == 0 and containerId in " |
|---|
| 390 | "(select id from fStorItems where (calcaction & 1) == 1 and " |
|---|
| 391 | "(calcaction & 16) == 0)") |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | self.tempDb.execSqlUntilNoChange("update fStorItems " |
|---|
| 396 | "set calcaction=calcaction|2|32 " |
|---|
| 397 | "where (calcaction & 2) == 0 and containerId in " |
|---|
| 398 | "(select id from fStorItems where (calcaction & 2) == 2)") |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | self.tempDb.execSql("update fStorItems set calcaction=calcaction & ~2 " |
|---|
| 402 | "where (calcaction & 3) == 3") |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | self.tempDb.execSql(""" |
|---|
| 410 | update fStorItems set calcaction = case |
|---|
| 411 | when (calcaction & 2) == 2 then 2 /* Delete */ |
|---|
| 412 | when (calcaction & 5) == 5 then 3 /* Collect */ |
|---|
| 413 | when (calcaction & 1) == 1 then 1 /* Keep */ |
|---|
| 414 | when (calcaction & 3) == 0 then ? /* Default */ |
|---|
| 415 | else 0 /* Internal error */ |
|---|
| 416 | end, |
|---|
| 417 | errormsg=''""", (orphanedActionDefault,)) |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | self.tempDb.commit() |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | def _deleteOrphanedItems(self): |
|---|
| 424 | """ |
|---|
| 425 | Delete orphaned items which where selected for deletion. Items are |
|---|
| 426 | deleted in the order of their deepness, meaning items with most path |
|---|
| 427 | elements first. Not the most efficient but simplest and safest way. |
|---|
| 428 | """ |
|---|
| 429 | for p in self.tempDb.execSqlQuerySingleColumn("select fullpath " |
|---|
| 430 | "from fStorItems where calcaction == 2 order by deepness desc"): |
|---|
| 431 | try: |
|---|
| 432 | deleteFile(p) |
|---|
| 433 | except: |
|---|
| 434 | traceback.print_exc() |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | def _pruneEmptyDirsFromFileStorage(self): |
|---|
| 438 | pass |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | def _listOrphanedFilesOnCollector(self, collectorPageName): |
|---|
| 444 | """ |
|---|
| 445 | Add links to collectorPageName of files which are set to be placed there |
|---|
| 446 | """ |
|---|
| 447 | absPaths = self.tempDb.execSqlQuerySingleColumn("select fullpath " |
|---|
| 448 | "from fStorItems where calcaction == 3 order by procCounter") |
|---|
| 449 | |
|---|
| 450 | if len(absPaths) == 0: |
|---|
| 451 | |
|---|
| 452 | return |
|---|
| 453 | |
|---|
| 454 | config = self.mainControl.getConfig() |
|---|
| 455 | |
|---|
| 456 | try: |
|---|
| 457 | prefix = StringOps.strftimeUB(StringOps.unescapeForIni(config.get( |
|---|
| 458 | "main", "editor_filePaste_prefix", u""))) |
|---|
| 459 | except: |
|---|
| 460 | traceback.print_exc() |
|---|
| 461 | prefix = u"" |
|---|
| 462 | |
|---|
| 463 | try: |
|---|
| 464 | middle = StringOps.strftimeUB(StringOps.unescapeForIni(config.get( |
|---|
| 465 | "main", "editor_filePaste_middle", u" "))) |
|---|
| 466 | except: |
|---|
| 467 | traceback.print_exc() |
|---|
| 468 | middle = u" " |
|---|
| 469 | |
|---|
| 470 | try: |
|---|
| 471 | suffix = StringOps.strftimeUB(StringOps.unescapeForIni(config.get( |
|---|
| 472 | "main", "editor_filePaste_suffix", u""))) |
|---|
| 473 | except: |
|---|
| 474 | traceback.print_exc() |
|---|
| 475 | suffix = u"" |
|---|
| 476 | |
|---|
| 477 | bracketedUrl = config.getboolean("main", |
|---|
| 478 | "editor_filePaste_bracketedUrl", True) |
|---|
| 479 | |
|---|
| 480 | langHelper = wx.GetApp().createWikiLanguageHelper( |
|---|
| 481 | self.wikiDocument.getWikiDefaultWikiLanguage()) |
|---|
| 482 | |
|---|
| 483 | urls = [langHelper.createUrlLinkFromPath(self.wikiDocument, ap, |
|---|
| 484 | relative=True, bracketed=bracketedUrl) for ap in absPaths] |
|---|
| 485 | |
|---|
| 486 | page = self.wikiDocument.getWikiPageNoError(collectorPageName) |
|---|
| 487 | page.appendLiveText(prefix + middle.join(urls) + suffix) |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | def runAfterDialog(self, collectorPageName): |
|---|
| 491 | """ |
|---|
| 492 | Apply actions after dialog was closed with "OK". IMPORTANT: |
|---|
| 493 | calcActionAndErrorsDuringDialog() must have been run for the final |
|---|
| 494 | db state before this function can be called. |
|---|
| 495 | """ |
|---|
| 496 | self._deleteOrphanedItems() |
|---|
| 497 | |
|---|
| 498 | self._listOrphanedFilesOnCollector(collectorPageName) |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | class _OrphanedGrid(EnhancedGrid): |
|---|
| 505 | |
|---|
| 506 | ACTIONCHOICELIST = None |
|---|
| 507 | CALCACTIONNAMES = None |
|---|
| 508 | |
|---|
| 509 | COLOR_GRAY = wx.Colour(200, 200, 200) |
|---|
| 510 | |
|---|
| 511 | ACTION_DEFAULT = 0 |
|---|
| 512 | ACTION_KEEP = 1 |
|---|
| 513 | ACTION_DELETE = 2 |
|---|
| 514 | ACTION_LINK_ON_COLLECTOR = 3 |
|---|
| 515 | |
|---|
| 516 | COL_RELPATH = 0 |
|---|
| 517 | COL_TYPE = 1 |
|---|
| 518 | COL_ACTION = 2 |
|---|
| 519 | COL_CALCACTION = 3 |
|---|
| 520 | |
|---|
| 521 | COL_COUNT = 4 |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | def __init__(self, parent, db, wikiDocument, collator, id=-1): |
|---|
| 525 | EnhancedGrid.__init__(self, parent, id) |
|---|
| 526 | |
|---|
| 527 | if _OrphanedGrid.ACTIONCHOICELIST is None: |
|---|
| 528 | _OrphanedGrid.ACTIONCHOICELIST = [_(u"Default"), _(u"Keep"), |
|---|
| 529 | _(u"Delete"), _(u"Collect")] |
|---|
| 530 | _OrphanedGrid.CALCACTIONNAMES = [_(u""), _(u"Keep"), |
|---|
| 531 | _(u"Delete"), _(u"Collect")] |
|---|
| 532 | |
|---|
| 533 | self.fileCleanupDialog = parent |
|---|
| 534 | self.db = db |
|---|
| 535 | self.wikiDocument = wikiDocument |
|---|
| 536 | self.collator = collator |
|---|
| 537 | |
|---|
| 538 | self.gridToId = [] |
|---|
| 539 | |
|---|
| 540 | |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | self.CreateGrid(0, self.COL_COUNT) |
|---|
| 553 | |
|---|
| 554 | self.SetColLabelValue(self.COL_RELPATH, _(u"Path")) |
|---|
| 555 | self.SetColLabelValue(self.COL_TYPE, _(u"Type")) |
|---|
| 556 | self.SetColLabelValue(self.COL_ACTION, _(u"Action")) |
|---|
| 557 | self.SetColLabelValue(self.COL_CALCACTION, _(u"Calc. action")) |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | colWidthSum = sum(self.GetColSize(i) for i in range(self.COL_COUNT)) |
|---|
| 561 | self.SetMinSize((min(colWidthSum + 40, 600), -1)) |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | readOnlyAttr = wx.grid.GridCellAttr() |
|---|
| 565 | readOnlyAttr.SetReadOnly() |
|---|
| 566 | readOnlyAttr.SetBackgroundColour(self.COLOR_GRAY) |
|---|
| 567 | |
|---|
| 568 | self.SetColAttr(self.COL_RELPATH, readOnlyAttr) |
|---|
| 569 | self.SetColAttr(self.COL_TYPE, readOnlyAttr) |
|---|
| 570 | self.SetColAttr(self.COL_CALCACTION, readOnlyAttr) |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | actionChEditor = wx.grid.GridCellChoiceEditor(self.ACTIONCHOICELIST, |
|---|
| 574 | False) |
|---|
| 575 | actionAttr = wx.grid.GridCellAttr() |
|---|
| 576 | actionAttr.SetEditor(actionChEditor) |
|---|
| 577 | |
|---|
| 578 | self.SetColAttr(self.COL_ACTION, actionAttr) |
|---|
| 579 | |
|---|
| 580 | self.updateGridBySql() |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | def _isDirectEdit(self, row, col): |
|---|
| 600 | return True |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | def OnGridSelectCell(self, evt): |
|---|
| 604 | evt.Skip() |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | def updateGridBySql(self, orphanedActionDefault=0): |
|---|
| 610 | dbData = self.db.getSqlDb().execSqlQuery( |
|---|
| 611 | "select relpath, type, id, action, calcaction, errormsg " |
|---|
| 612 | "from fStorItems") |
|---|
| 613 | |
|---|
| 614 | self.collator.sortByFirst(dbData) |
|---|
| 615 | |
|---|
| 616 | actDef = _(u"Default") |
|---|
| 617 | typStr = [_(u"File"), _(u"Directory")] |
|---|
| 618 | |
|---|
| 619 | if self.GetNumberRows() > 0: |
|---|
| 620 | self.DeleteRows(0, self.GetNumberRows()) |
|---|
| 621 | self.AppendRows(len(dbData)) |
|---|
| 622 | |
|---|
| 623 | self.gridToId = [] |
|---|
| 624 | |
|---|
| 625 | for rowNo, (relPath, typ, id, action, calcaction, errormsg) \ |
|---|
| 626 | in enumerate(dbData): |
|---|
| 627 | self.gridToId.append(id) |
|---|
| 628 | |
|---|
| 629 | self.SetCellValue(rowNo, self.COL_RELPATH, relPath) |
|---|
| 630 | self.SetCellValue(rowNo, self.COL_TYPE, typStr[typ]) |
|---|
| 631 | self.SetCellValue(rowNo, self.COL_ACTION, |
|---|
| 632 | self.ACTIONCHOICELIST[action]) |
|---|
| 633 | self.SetCellValue(rowNo, self.COL_CALCACTION, |
|---|
| 634 | self.CALCACTIONNAMES[calcaction]) |
|---|
| 635 | |
|---|
| 636 | if calcaction != 0 and orphanedActionDefault != 0: |
|---|
| 637 | if action == 0: |
|---|
| 638 | action = orphanedActionDefault |
|---|
| 639 | if action != calcaction: |
|---|
| 640 | self.SetCellBackgroundColour(rowNo, self.COL_CALCACTION, wx.RED) |
|---|
| 641 | else: |
|---|
| 642 | self.SetCellBackgroundColour(rowNo, self.COL_CALCACTION, |
|---|
| 643 | self.COLOR_GRAY) |
|---|
| 644 | else: |
|---|
| 645 | self.SetCellBackgroundColour(rowNo, self.COL_CALCACTION, |
|---|
| 646 | self.COLOR_GRAY) |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | def storeGridToSql(self): |
|---|
| 651 | sqlDb = self.db.getSqlDb() |
|---|
| 652 | |
|---|
| 653 | for rowNo, id in enumerate(self.gridToId): |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | sqlDb.execSql("update fStorItems set action=? where id==?", |
|---|
| 658 | (self.ACTIONCHOICELIST.index(self.GetCellValue(rowNo, |
|---|
| 659 | self.COL_ACTION)), id)) |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | class _MissingGrid(EnhancedGrid): |
|---|
| 667 | |
|---|
| 668 | ACTIONCHOICELIST = None |
|---|
| 669 | |
|---|
| 670 | COLOR_GRAY = wx.Colour(200, 200, 200) |
|---|
| 671 | |
|---|
| 672 | ACTION_DEFAULT = 0 |
|---|
| 673 | ACTION_KEEP = 1 |
|---|
| 674 | ACTION_DELETE = 2 |
|---|
| 675 | |
|---|
| 676 | COL_FULLPATH = 0 |
|---|
| 677 | COL_ACTION = 1 |
|---|
| 678 | |
|---|
| 679 | COL_COUNT = 2 |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | def __init__(self, parent, db, wikiDocument, collator, id=-1): |
|---|
| 683 | EnhancedGrid.__init__(self, parent, id) |
|---|
| 684 | |
|---|
| 685 | if _MissingGrid.ACTIONCHOICELIST is None: |
|---|
| 686 | _MissingGrid.ACTIONCHOICELIST = [_(u"Default"), _(u"Keep"), |
|---|
| 687 | _(u"Delete")] |
|---|
| 688 | |
|---|
| 689 | self.fileCleanupDialog = parent |
|---|
| 690 | self.db = db |
|---|
| 691 | self.wikiDocument = wikiDocument |
|---|
| 692 | self.collator = collator |
|---|
| 693 | |
|---|
| 694 | self.gridToId = [] |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | self.CreateGrid(0, self.COL_COUNT) |
|---|
| 709 | |
|---|
| 710 | self.SetColLabelValue(self.COL_FULLPATH, _(u"Path")) |
|---|
| 711 | self.SetColLabelValue(self.COL_ACTION, _(u"Action")) |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | colWidthSum = sum(self.GetColSize(i) for i in range(self.COL_COUNT)) |
|---|
| 715 | self.SetMinSize((min(colWidthSum + 40, 600), -1)) |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | readOnlyAttr = wx.grid.GridCellAttr() |
|---|
| 719 | readOnlyAttr.SetReadOnly() |
|---|
| 720 | readOnlyAttr.SetBackgroundColour(self.COLOR_GRAY) |
|---|
| 721 | |
|---|
| 722 | self.SetColAttr(self.COL_FULLPATH, readOnlyAttr) |
|---|
| 723 | |
|---|
| 724 | actionChEditor = wx.grid.GridCellChoiceEditor(self.ACTIONCHOICELIST, |
|---|
| 725 | False) |
|---|
| 726 | actionAttr = wx.grid.GridCellAttr() |
|---|
| 727 | actionAttr.SetEditor(actionChEditor) |
|---|
| 728 | |
|---|
| 729 | self.SetColAttr(self.COL_ACTION, actionAttr) |
|---|
| 730 | |
|---|
| 731 | self.updateGridBySql() |
|---|
| 732 | |
|---|
| 733 | self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell) |
|---|
| 734 | self.Bind(wx.grid.EVT_GRID_CMD_CELL_LEFT_DCLICK, self.OnCellDClick) |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | def _isDirectEdit(self, row, col): |
|---|
| 753 | return True |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | def OnSelectCell(self, evt): |
|---|
| 757 | self.fileCleanupDialog.updateMissingLinkingPagesListBoxByRefedItemId( |
|---|
| 758 | self.gridToId[evt.GetRow()]) |
|---|
| 759 | evt.Skip() |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | def OnCellDClick(self, evt): |
|---|
| 765 | self.fileCleanupDialog.runFirstInMissingLinkingPagesListBox() |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | def updateGridBySql(self): |
|---|
| 769 | dbData = self.db.getSqlDb().execSqlQuery( |
|---|
| 770 | "select fullpath, id, present, action " |
|---|
| 771 | "from refedItems") |
|---|
| 772 | |
|---|
| 773 | self.collator.sortByFirst(dbData) |
|---|
| 774 | |
|---|
| 775 | actDef = _(u"Default") |
|---|
| 776 | |
|---|
| 777 | row = self.GetGridCursorRow() |
|---|
| 778 | col = self.GetGridCursorCol() |
|---|
| 779 | |
|---|
| 780 | if self.GetNumberRows() > 0: |
|---|
| 781 | self.DeleteRows(0, self.GetNumberRows()) |
|---|
| 782 | self.AppendRows(len(dbData)) |
|---|
| 783 | |
|---|
| 784 | self.gridToId = [] |
|---|
| 785 | |
|---|
| 786 | for rowNo, (fullPath, id, present, action) \ |
|---|
| 787 | in enumerate(dbData): |
|---|
| 788 | self.gridToId.append(id) |
|---|
| 789 | |
|---|
| 790 | self.SetCellValue(rowNo, self.COL_FULLPATH, fullPath) |
|---|
| 791 | self.SetCellValue(rowNo, self.COL_ACTION, |
|---|
| 792 | self.ACTIONCHOICELIST[action]) |
|---|
| 793 | |
|---|
| 794 | self.SetGridCursor(row, col) |
|---|
| 795 | |
|---|
| 796 | |
|---|
| 797 | def storeGridToSql(self): |
|---|
| 798 | sqlDb = self.db.getSqlDb() |
|---|
| 799 | |
|---|
| 800 | for rowNo, id in enumerate(self.gridToId): |
|---|
| 801 | |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | sqlDb.execSql("update refedItems set action=? where id==?", |
|---|
| 805 | (self.ACTIONCHOICELIST.index(self.GetCellValue(rowNo, |
|---|
| 806 | self.COL_ACTION)), id)) |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | class _MissingLinkingPagesItemInfo(object): |
|---|
| 812 | __slots__ = ("__weakref__", "unifName", "wikiWord", "hitList", |
|---|
| 813 | "fileCleanupDialog") |
|---|
| 814 | |
|---|
| 815 | def __init__(self, fileCleanupDialog, unifName, hitList): |
|---|
| 816 | self.fileCleanupDialog = fileCleanupDialog |
|---|
| 817 | |
|---|
| 818 | self.unifName = unifName |
|---|
| 819 | if unifName.startswith(u"wikipage/"): |
|---|
| 820 | self.wikiWord = unifName[9:] |
|---|
| 821 | else: |
|---|
| 822 | self.wikiWord = unifName |
|---|
| 823 | |
|---|
| 824 | hitList.sort() |
|---|
| 825 | self.hitList = hitList |
|---|
| 826 | |
|---|
| 827 | |
|---|
| 828 | |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | |
|---|
| 842 | |
|---|
| 843 | |
|---|
| 844 | |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | |
|---|
| 854 | |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | |
|---|
| 860 | |
|---|
| 861 | |
|---|
| 862 | def getCharSelection(self): |
|---|
| 863 | if len(self.hitList) == 0: |
|---|
| 864 | return (-1, -1) |
|---|
| 865 | |
|---|
| 866 | return (self.hitList[0][0], self.hitList[0][1]) |
|---|
| 867 | |
|---|
| 868 | |
|---|
| 869 | def getHtml(self): |
|---|
| 870 | modified = self.fileCleanupDialog.isModifiedPage(self.unifName) |
|---|
| 871 | |
|---|
| 872 | if modified: |
|---|
| 873 | result = [u'<font color="GRAY"><b>%s</b>' % \ |
|---|
| 874 | StringOps.escapeHtml(self.wikiWord)] |
|---|
| 875 | else: |
|---|
| 876 | result = [u'<font color="BLUE"><b>%s</b></font>' % \ |
|---|
| 877 | StringOps.escapeHtml(self.wikiWord)] |
|---|
| 878 | |
|---|
| 879 | if len(self.hitList) > 4: |
|---|
| 880 | for hit in self.hitList[:3]: |
|---|
| 881 | result.append(u"<br />\n%s" % StringOps.escapeHtml(hit[2])) |
|---|
| 882 | |
|---|
| 883 | result.append(u"<br />\n...") |
|---|
| 884 | else: |
|---|
| 885 | for hit in self.hitList: |
|---|
| 886 | result.append(u"<br />\n%s" % StringOps.escapeHtml(hit[2])) |
|---|
| 887 | |
|---|
| 888 | if modified: |
|---|
| 889 | result.append(u"</font>") |
|---|
| 890 | |
|---|
| 891 | return u"".join(result) |
|---|
| 892 | |
|---|
| 893 | |
|---|
| 894 | |
|---|
| 895 | |
|---|
| 896 | class _MissingLinkingPagesListBox(wx.HtmlListBox): |
|---|
| 897 | def __init__(self, parent, db, mainControl, collator, ID): |
|---|
| 898 | wx.HtmlListBox.__init__(self, parent, ID, style = wx.SUNKEN_BORDER) |
|---|
| 899 | |
|---|
| 900 | self.fileCleanupDialog = parent |
|---|
| 901 | self.db = db |
|---|
| 902 | self.mainControl = mainControl |
|---|
| 903 | self.collator = collator |
|---|
| 904 | self.lastRefedItemId = -1 |
|---|
| 905 | |
|---|
| 906 | self.itemInfo = [] |
|---|
| 907 | self.SetItemCount(0) |
|---|
| 908 | |
|---|
| 909 | wx.EVT_KILL_FOCUS(self, self.OnKillFocus) |
|---|
| 910 | wx.EVT_LISTBOX_DCLICK(self, ID, self.OnDClick) |
|---|
| 911 | |
|---|
| 912 | |
|---|
| 913 | |
|---|
| 914 | |
|---|
| 915 | |
|---|
| 916 | |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | |
|---|
| 926 | |
|---|
| 927 | |
|---|
| 928 | def OnKillFocus(self, evt): |
|---|
| 929 | self.SetSelection(-1) |
|---|
| 930 | |
|---|
| 931 | |
|---|
| 932 | def OnGetItem(self, i): |
|---|
| 933 | |
|---|
| 934 | |
|---|
| 935 | |
|---|
| 936 | |
|---|
| 937 | |
|---|
| 938 | try: |
|---|
| 939 | return self.itemInfo[i].getHtml() |
|---|
| 940 | except IndexError: |
|---|
| 941 | return u"" |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | def updateByRefedItemId(self, refedItemId): |
|---|
| 945 | self.lastRefedItemId = refedItemId |
|---|
| 946 | dbData = self.db.getSqlDb().execSqlQuery("select unifName, " |
|---|
| 947 | "corePos, coreLength, presentationUrl from unifNameToItem " |
|---|
| 948 | "where refedItemId == ?", (refedItemId,)) |
|---|
| 949 | |
|---|
| 950 | if len(dbData) == 0: |
|---|
| 951 | self.SetItemCount(0) |
|---|
| 952 | return |
|---|
| 953 | |
|---|
| 954 | |
|---|
| 955 | groupDict = {} |
|---|
| 956 | for un, cp, cl, pu in dbData: |
|---|
| 957 | groupDict.setdefault(un, []).append((cp, cl, pu)) |
|---|
| 958 | |
|---|
| 959 | finalData = groupDict.items() |
|---|
| 960 | |
|---|
| 961 | self.collator.sortByFirst(finalData) |
|---|
| 962 | |
|---|
| 963 | self.itemInfo = [_MissingLinkingPagesItemInfo(self.fileCleanupDialog, |
|---|
| 964 | un, hitList) for un, hitList in finalData] |
|---|
| 965 | |
|---|
| 966 | self.SetItemCount(len(self.itemInfo)) |
|---|
| 967 | self.Refresh() |
|---|
| 968 | |
|---|
| 969 | |
|---|
| 970 | def update(self): |
|---|
| 971 | if self.lastRefedItemId > -1: |
|---|
| 972 | self.updateByRefedItemId(self.lastRefedItemId) |
|---|
| 973 | |
|---|
| 974 | |
|---|
| 975 | def activateSelection(self, sel): |
|---|
| 976 | if sel == -1 or self.GetItemCount() == 0: |
|---|
| 977 | return |
|---|
| 978 | |
|---|
| 979 | info = self.itemInfo[sel] |
|---|
| 980 | |
|---|
| 981 | self.mainControl.openWikiPage(info.wikiWord) |
|---|
| 982 | |
|---|
| 983 | editor = self.mainControl.getActiveEditor() |
|---|
| 984 | if editor is not None: |
|---|
| 985 | csel = info.getCharSelection() |
|---|
| 986 | if csel[0] != -1: |
|---|
| 987 | self.mainControl.getActiveEditor().showSelectionByCharPos( |
|---|
| 988 | csel[0], csel[0] + csel[1]) |
|---|
| 989 | |
|---|
| 990 | |
|---|
| 991 | |
|---|
| 992 | |
|---|
| 993 | editor.SetFocus() |
|---|
| 994 | editor.SetFocus() |
|---|
| 995 | |
|---|
| 996 | |
|---|
| 997 | def OnDClick(self, evt): |
|---|
| 998 | self.activateSelection(self.GetSelection()) |
|---|
| 999 | |
|---|
| 1000 | |
|---|
| 1001 | |
|---|
| 1002 | |
|---|
| 1003 | |
|---|
| 1004 | |
|---|
| 1005 | |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | |
|---|
| 1009 | |
|---|
| 1010 | |
|---|
| 1011 | |
|---|
| 1012 | |
|---|
| 1013 | |
|---|
| 1014 | |
|---|
| 1015 | |
|---|
| 1016 | |
|---|
| 1017 | |
|---|
| 1018 | |
|---|
| 1019 | |
|---|
| 1020 | |
|---|
| 1021 | |
|---|
| 1022 | |
|---|
| 1023 | |
|---|
| 1024 | |
|---|
| 1025 | |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | |
|---|
| 1030 | |
|---|
| 1031 | |
|---|
| 1032 | |
|---|
| 1033 | |
|---|
| 1034 | |
|---|
| 1035 | |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | |
|---|
| 1039 | |
|---|
| 1040 | |
|---|
| 1041 | |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | |
|---|
| 1047 | |
|---|
| 1048 | |
|---|
| 1049 | |
|---|
| 1050 | |
|---|
| 1051 | |
|---|
| 1052 | |
|---|
| 1053 | |
|---|
| 1054 | |
|---|
| 1055 | |
|---|
| 1056 | |
|---|
| 1057 | |
|---|
| 1058 | |
|---|
| 1059 | |
|---|
| 1060 | |
|---|
| 1061 | |
|---|
| 1062 | |
|---|
| 1063 | |
|---|
| 1064 | |
|---|
| 1065 | |
|---|
| 1066 | |
|---|
| 1067 | |
|---|
| 1068 | |
|---|
| 1069 | |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | |
|---|
| 1073 | |
|---|
| 1074 | |
|---|
| 1075 | |
|---|
| 1076 | |
|---|
| 1077 | |
|---|
| 1078 | |
|---|
| 1079 | |
|---|
| 1080 | |
|---|
| 1081 | |
|---|
| 1082 | |
|---|
| 1083 | |
|---|
| 1084 | |
|---|
| 1085 | |
|---|
| 1086 | |
|---|
| 1087 | |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | |
|---|
| 1091 | |
|---|
| 1092 | |
|---|
| 1093 | |
|---|
| 1094 | |
|---|
| 1095 | |
|---|
| 1096 | |
|---|
| 1097 | |
|---|
| 1098 | |
|---|
| 1099 | |
|---|
| 1100 | |
|---|
| 1101 | |
|---|
| 1102 | |
|---|
| 1103 | |
|---|
| 1104 | |
|---|
| 1105 | |
|---|
| 1106 | |
|---|
| 1107 | |
|---|
| 1108 | |
|---|
| 1109 | |
|---|
| 1110 | |
|---|
| 1111 | |
|---|
| 1112 | |
|---|
| 1113 | |
|---|
| 1114 | |
|---|
| 1115 | |
|---|
| 1116 | |
|---|
| 1117 | |
|---|
| 1118 | |
|---|
| 1119 | |
|---|
| 1120 | |
|---|
| 1121 | |
|---|
| 1122 | |
|---|
| 1123 | |
|---|
| 1124 | |
|---|
| 1125 | |
|---|
| 1126 | |
|---|
| 1127 | |
|---|
| 1128 | |
|---|
| 1129 | |
|---|
| 1130 | |
|---|
| 1131 | |
|---|
| 1132 | |
|---|
| 1133 | |
|---|
| 1134 | |
|---|
| 1135 | |
|---|
| 1136 | |
|---|
| 1137 | |
|---|
| 1138 | |
|---|
| 1139 | |
|---|
| 1140 | |
|---|
| 1141 | |
|---|
| 1142 | |
|---|
| 1143 | |
|---|
| 1144 | |
|---|
| 1145 | |
|---|
| 1146 | |
|---|
| 1147 | |
|---|
| 1148 | |
|---|
| 1149 | |
|---|
| 1150 | |
|---|
| 1151 | |
|---|
| 1152 | |
|---|
| 1153 | |
|---|
| 1154 | |
|---|
| 1155 | |
|---|
| 1156 | |
|---|
| 1157 | |
|---|
| 1158 | |
|---|
| 1159 | |
|---|
| 1160 | |
|---|
| 1161 | |
|---|
| 1162 | |
|---|
| 1163 | |
|---|
| 1164 | |
|---|
| 1165 | |
|---|
| 1166 | |
|---|
| 1167 | |
|---|
| 1168 | |
|---|
| 1169 | |
|---|
| 1170 | |
|---|
| 1171 | |
|---|
| 1172 | |
|---|
| 1173 | |
|---|
| 1174 | |
|---|
| 1175 | |
|---|
| 1176 | |
|---|
| 1177 | |
|---|
| 1178 | |
|---|
| 1179 | |
|---|
| 1180 | |
|---|
| 1181 | |
|---|
| 1182 | |
|---|
| 1183 | |
|---|
| 1184 | |
|---|
| 1185 | |
|---|
| 1186 | |
|---|
| 1187 | |
|---|
| 1188 | |
|---|
| 1189 | |
|---|
| 1190 | |
|---|
| 1191 | |
|---|
| 1192 | |
|---|
| 1193 | |
|---|
| 1194 | |
|---|
| 1195 | |
|---|
| 1196 | |
|---|
| 1197 | |
|---|
| 1198 | |
|---|
| 1199 | |
|---|
| 1200 | |
|---|
| 1201 | |
|---|
| 1202 | |
|---|
| 1203 | class FileCleanupInitialDialog(wx.Dialog): |
|---|
| 1204 | """ |
|---|
| 1205 | Dialog to ask for the options for the data to collect on file links |
|---|
| 1206 | and file storage |
|---|
| 1207 | """ |
|---|
| 1208 | |
|---|
| 1209 | def __init__(self, mainControl, parent): |
|---|
| 1210 | d = wx.PreDialog() |
|---|
| 1211 | self.PostCreate(d) |
|---|
| 1212 | |
|---|
| 1213 | self.mainControl = mainControl |
|---|
| 1214 | self.value = None |
|---|
| 1215 | |
|---|
| 1216 | res = wx.xrc.XmlResource.Get() |
|---|
| 1217 | res.LoadOnDialog(self, parent, "FileCleanupInitialDialog") |
|---|
| 1218 | |
|---|
| 1219 | self.ctrls = XrcControls(self) |
|---|
| 1220 | |
|---|
| 1221 | value = {"downwardRef": True} |
|---|
| 1222 | self.ctrls.cbDownwardRef.SetValue(value["downwardRef"]) |
|---|
| 1223 | |
|---|
| 1224 | self.ctrls.btnOk.SetId(wx.ID_OK) |
|---|
| 1225 | self.ctrls.btnCancel.SetId(wx.ID_CANCEL) |
|---|
| 1226 | |
|---|
| 1227 | self.Fit() |
|---|
| 1228 | |
|---|
| 1229 | |
|---|
| 1230 | self.SetFocus() |
|---|
| 1231 | |
|---|
| 1232 | wx.EVT_BUTTON(self, wx.ID_OK, self.OnOk) |
|---|
| 1233 | |
|---|
| 1234 | |
|---|
| 1235 | def GetValue(self): |
|---|
| 1236 | return self.value |
|---|
| 1237 | |
|---|
| 1238 | |
|---|
| 1239 | def OnOk(self, evt): |
|---|
| 1240 | self.value = {"downwardRef": self.ctrls.cbDownwardRef.GetValue()} |
|---|
| 1241 | |
|---|
| 1242 | self.EndModal(wx.ID_OK) |
|---|
| 1243 | |
|---|
| 1244 | FileCleanupInitialDialog.runModal = staticmethod(runDialogModalFactory( |
|---|
| 1245 | FileCleanupInitialDialog)) |
|---|
| 1246 | |
|---|
| 1247 | |
|---|
| 1248 | |
|---|
| 1249 | |
|---|
| 1250 | class FileCleanupDialog(wx.Dialog): |
|---|
| 1251 | """ |
|---|
| 1252 | """ |
|---|
| 1253 | |
|---|
| 1254 | |
|---|
| 1255 | ORPHANED_DEFAULTACTIONCHOICELIST = None |
|---|
| 1256 | |
|---|
| 1257 | def __init__(self, mainControl, db, parent): |
|---|
| 1258 | |
|---|
| 1259 | if FileCleanupDialog.ORPHANED_DEFAULTACTIONCHOICELIST is None: |
|---|
| 1260 | FileCleanupDialog.ORPHANED_DEFAULTACTIONCHOICELIST = [_(u"Keep"), |
|---|
| 1261 | _(u"Delete"), _(u"Collect")] |
|---|
| 1262 | |
|---|
| 1263 | d = wx.PreDialog() |
|---|
| 1264 | self.PostCreate(d) |
|---|
| 1265 | |
|---|
| 1266 | self.mainControl = mainControl |
|---|
| 1267 | self.db = db |
|---|
| 1268 | self.value = False |
|---|
| 1269 | |
|---|
| 1270 | self.modifiedSinceOpen = set() |
|---|
| 1271 | |
|---|
| 1272 | |
|---|
| 1273 | res = wx.xrc.XmlResource.Get() |
|---|
| 1274 | res.LoadOnDialog(self, parent, "FileCleanupDialog") |
|---|
| 1275 | |
|---|
| 1276 | self.ctrls = XrcControls(self) |
|---|
| 1277 | |
|---|
| 1278 | orphanedGrid = _OrphanedGrid(self, self.db, mainControl.getWikiDocument(), |
|---|
| 1279 | mainControl.getCollator()) |
|---|
| 1280 | |
|---|
| 1281 | res.AttachUnknownControl("gridOrphaned", orphanedGrid, self) |
|---|
| 1282 | |
|---|
| 1283 | missingGrid = _MissingGrid(self, self.db, mainControl.getWikiDocument(), |
|---|
| 1284 | mainControl.getCollator()) |
|---|
| 1285 | |
|---|
| 1286 | res.AttachUnknownControl("gridMissing", missingGrid, self) |
|---|
| 1287 | |
|---|
| 1288 | htmllbMissingLinkingPages = _MissingLinkingPagesListBox(self, self.db, |
|---|
| 1289 | mainControl, mainControl.getCollator(), |
|---|
| 1290 | GUI_ID.htmllbMissingLinkingPages) |
|---|
| 1291 | |
|---|
| 1292 | res.AttachUnknownControl("htmllbMissingLinkingPages", |
|---|
| 1293 | htmllbMissingLinkingPages, self) |
|---|
| 1294 | |
|---|
| 1295 | self.ctrls.btnOk.SetId(wx.ID_OK) |
|---|
| 1296 | self.ctrls.btnCancel.SetId(wx.ID_CANCEL) |
|---|
| 1297 | |
|---|
| 1298 | self.Fit() |
|---|
| 1299 | |
|---|
| 1300 | WindowLayout.setWindowSize(self) |
|---|
| 1301 | WindowLayout.setWindowPos(self) |
|---|
| 1302 | |
|---|
| 1303 | |
|---|
| 1304 | orphanedGrid.GetGrandParent().Layout() |
|---|
| 1305 | missingGrid.GetGrandParent().Layout() |
|---|
| 1306 | |
|---|
| 1307 | |
|---|
| 1308 | self.SetFocus() |
|---|
| 1309 | |
|---|
| 1310 | wx.EVT_BUTTON(self, wx.ID_OK, self.OnOk) |
|---|
| 1311 | wx.EVT_BUTTON(self, GUI_ID.btnTest, self.OnTest) |
|---|
| 1312 | |
|---|
| 1313 | |
|---|
| 1314 | self.__sinkWikiDoc = wxKeyFunctionSink(( |
|---|
| 1315 | ("renamed wiki page", self.onRemovedWikiPage), |
|---|
| 1316 | ("deleted wiki page", self.onRemovedWikiPage), |
|---|
| 1317 | ("updated wiki page", self.onModifiedWikiPage), |
|---|
| 1318 | ), self.mainControl.getWikiDocument().getMiscEvent(), self) |
|---|
| 1319 | |
|---|
| 1320 | |
|---|
| 1321 | |
|---|
| 1322 | |
|---|
| 1323 | |
|---|
| 1324 | |
|---|
| 1325 | |
|---|
| 1326 | |
|---|
| 1327 | |
|---|
| 1328 | |
|---|
| 1329 | def GetValue(self): |
|---|
| 1330 | return self.value |
|---|
| 1331 | |
|---|
| 1332 | |
|---|
| 1333 | def OnOk(self, evt): |
|---|
| 1334 | |
|---|
| 1335 | |
|---|
| 1336 | |
|---|
| 1337 | |
|---|
| 1338 | |
|---|
| 1339 | self.ctrls.gridOrphaned.storeGridToSql() |
|---|
| 1340 | self.db.calcActionAndErrorsDuringDialog( |
|---|
| 1341 | self.ctrls.chOrphanedDefaultAction.GetSelection() + 1, |
|---|
| 1342 | self.ctrls.cbOrphanedDownwardRef.GetValue()) |
|---|
| 1343 | |
|---|
| 1344 | self.db.getSqlDb().commit() |
|---|
| 1345 | |
|---|
| 1346 | absPaths = self.db.getSqlDb().execSqlQuerySingleColumn("select fullpath " |
|---|
| 1347 | "from fStorItems where calcaction == 3 limit 1") |
|---|
| 1348 | |
|---|
| 1349 | collectorPageName = self.ctrls.tfOrphanedCollectorPage.GetValue() |
|---|
| 1350 | |
|---|
| 1351 | if len(absPaths) > 0: |
|---|
| 1352 | |
|---|
| 1353 | langHelper = wx.GetApp().createWikiLanguageHelper( |
|---|
| 1354 | self.mainControl.getWikiDocument()\ |
|---|
| 1355 | .getWikiDefaultWikiLanguage()) |
|---|
| 1356 | |
|---|
| 1357 | if langHelper.checkForInvalidWikiWord(collectorPageName, |
|---|
| 1358 | self.mainControl.getWikiDocument()) is not None: |
|---|
| 1359 | |
|---|
| 1360 | |
|---|
| 1361 | self.OnTest(None) |
|---|
| 1362 | return |
|---|
| 1363 | |
|---|
| 1364 | self.value = True |
|---|
| 1365 | |
|---|
| 1366 | self.db.runAfterDialog(collectorPageName) |
|---|
| 1367 | |
|---|
| 1368 | if self is self.mainControl.nonModalFileCleanupDlg: |
|---|
| 1369 | self.mainControl.nonModalFileCleanupDlg = None |
|---|
| 1370 | |
|---|
| 1371 | self.__sinkWikiDoc.disconnect() |
|---|
| 1372 | |
|---|
| 1373 | if self.IsModal(): |
|---|
| 1374 | self.EndModal(wx.ID_OK) |
|---|
| 1375 | else: |
|---|
| 1376 | self.Destroy() |
|---|
| 1377 | |
|---|
| 1378 | |
|---|
| 1379 | |
|---|
| 1380 | def OnClose(self, evt): |
|---|
| 1381 | self.value = None |
|---|
| 1382 | |
|---|
| 1383 | if self is self.mainControl.nonModalFileCleanupDlg: |
|---|
| 1384 | self.mainControl.nonModalFileCleanupDlg = None |
|---|
| 1385 | |
|---|
| 1386 | self.__sinkWikiDoc.disconnect() |
|---|
| 1387 | |
|---|
| 1388 | if self.IsModal(): |
|---|
| 1389 | self.EndModal(wx.ID_CANCEL) |
|---|
| 1390 | else: |
|---|
| 1391 | self.Destroy() |
|---|
| 1392 | |
|---|
| 1393 | |
|---|
| 1394 | def OnTest(self, evt): |
|---|
| 1395 | self.ctrls.gridOrphaned.storeGridToSql() |
|---|
| 1396 | self.db.calcActionAndErrorsDuringDialog( |
|---|
| 1397 | self.ctrls.chOrphanedDefaultAction.GetSelection() + 1, |
|---|
| 1398 | self.ctrls.cbOrphanedDownwardRef.GetValue()) |
|---|
| 1399 | |
|---|
| 1400 | self.db.getSqlDb().commit() |
|---|
| 1401 | |
|---|
| 1402 | absPaths = self.db.getSqlDb().execSqlQuerySingleColumn("select fullpath " |
|---|
| 1403 | "from fStorItems where calcaction == 3 limit 1") |
|---|
| 1404 | |
|---|
| 1405 | self.ctrls.tfOrphanedCollectorPage.SetBackgroundColour(wx.WHITE) |
|---|
| 1406 | |
|---|
| 1407 | collectorPageName = self.ctrls.tfOrphanedCollectorPage.GetValue() |
|---|
| 1408 | |
|---|
| 1409 | if len(absPaths) > 0: |
|---|
| 1410 | |
|---|
| 1411 | langHelper = wx.GetApp().createWikiLanguageHelper( |
|---|
| 1412 | self.mainControl.getWikiDocument()\ |
|---|
| 1413 | .getWikiDefaultWikiLanguage()) |
|---|
| 1414 | |
|---|
| 1415 | if langHelper.checkForInvalidWikiWord(collectorPageName, |
|---|
| 1416 | self.mainControl.getWikiDocument()) is not None: |
|---|
| 1417 | self.ctrls.tfOrphanedCollectorPage.SetBackgroundColour(wx.RED) |
|---|
| 1418 | |
|---|
| 1419 | self.ctrls.tfOrphanedCollectorPage.Refresh() |
|---|
| 1420 | |
|---|
| 1421 | self.ctrls.gridOrphaned.updateGridBySql( |
|---|
| 1422 | self.ctrls.chOrphanedDefaultAction.GetSelection() + 1) |
|---|
| 1423 | |
|---|
| 1424 | return |
|---|
| 1425 | |
|---|
| 1426 | |
|---|
| 1427 | def onModifiedWikiPage(self, miscevt): |
|---|
| 1428 | self.db.updateWikiPage(miscevt.get("wikiPage")) |
|---|
| 1429 | self.db.deleteUninteresting() |
|---|
| 1430 | |
|---|
| 1431 | |
|---|
| 1432 | |
|---|
| 1433 | |
|---|
| 1434 | |
|---|
| 1435 | self.ctrls.gridMissing.updateGridBySql() |
|---|
| 1436 | self.ctrls.htmllbMissingLinkingPages.update() |
|---|
| 1437 | |
|---|
| 1438 | |
|---|
| 1439 | def onRemovedWikiPage(self, miscevt): |
|---|
| 1440 | if miscevt.get("wikiPage").getUnifiedPageName() in self.modifiedSinceOpen: |
|---|
| 1441 | return |
|---|
| 1442 | |
|---|
| 1443 | self.modifiedSinceOpen.add(miscevt.get("wikiPage").getUnifiedPageName()) |
|---|
| 1444 | self.ctrls.htmllbMissingLinkingPages.update() |
|---|
| 1445 | |
|---|
| 1446 | |
|---|
| 1447 | def isModifiedPage(self, unifPageName): |
|---|
| 1448 | return unifPageName in self.modifiedSinceOpen |
|---|
| 1449 | |
|---|
| 1450 | def updateMissingLinkingPagesListBoxByRefedItemId(self, refedItemId): |
|---|
| 1451 | self.ctrls.htmllbMissingLinkingPages.updateByRefedItemId(refedItemId) |
|---|
| 1452 | |
|---|
| 1453 | def runFirstInMissingLinkingPagesListBox(self): |
|---|
| 1454 | """ |
|---|
| 1455 | Simulate double click on first item in MissingLinkingPagesListBox |
|---|
| 1456 | Called after double click on a cell in missing items table |
|---|
| 1457 | """ |
|---|
| 1458 | self.ctrls.htmllbMissingLinkingPages.activateSelection(0) |
|---|
| 1459 | |
|---|
| 1460 | |
|---|
| 1461 | |
|---|
| 1462 | FileCleanupDialog.runModal = staticmethod(runDialogModalFactory( |
|---|
| 1463 | FileCleanupDialog)) |
|---|
| 1464 | |
|---|
| 1465 | |
|---|
| 1466 | |
|---|
| 1467 | |
|---|
| 1468 | def runFileCleanup(mainControl, parent, progresshandler): |
|---|
| 1469 | if mainControl.nonModalFileCleanupDlg: |
|---|
| 1470 | |
|---|
| 1471 | mainControl.nonModalFileCleanupDlg.SetFocus() |
|---|
| 1472 | return |
|---|
| 1473 | |
|---|
| 1474 | options = FileCleanupInitialDialog.runModal(mainControl, parent) |
|---|
| 1475 | if options is None: |
|---|
| 1476 | return |
|---|
| 1477 | |
|---|
| 1478 | db = InfoDatabase(mainControl) |
|---|
| 1479 | db.buildDatabaseBeforeDialog(progresshandler, options) |
|---|
| 1480 | |
|---|
| 1481 | dlg = FileCleanupDialog(mainControl, db, parent) |
|---|
| 1482 | mainControl.nonModalFileCleanupDlg = dlg |
|---|
| 1483 | |
|---|
| 1484 | |
|---|
| 1485 | |
|---|
| 1486 | dlg.Show() |
|---|
| 1487 | |
|---|
| 1488 | |
|---|